[Python] Defining a 2D object and using its area as Boolean

Discussão em 'Python' iniciado por Stack, Junho 10, 2025.

  1. Stack

    Stack Membro Participativo

    I have defined two space dimesions ( x and z ) and I was able to manually "draw" an object to use it as a boolen for solving an equation. I defined it as it follows:

    A = np.zeros((nz,nx))
    object = np.ones_like(A)
    object[ int(5/dz):int(10/dz) , int(5/dx):int(10/dz) ] = 2
    object = object == 2


    By doing that I can define an square 5x10 in z dimesion and 5x10 in x dimesion , and apply the algorythim which understands this as an area , I think. But when it comes to draw complex areas it ends up being hard doing it by little squares and rectangles.

    So I want to automatize an area generation by mouse clicking and I want to be able to use this area as a boolean.

    I was able to draw a polygon using:

    import matplotlib.pyplot as plt
    import numpy as np
    from matplotlib.patches import Polygon

    fig, ax = plt.subplots()

    object = np.array(plt.ginput(n=-100,mouse_stop=2))
    p = Polygon(object, alpha=0.5)
    plt.gca().add_artist(p)


    plt.draw()
    plt.show()


    But this outputs z and x coordinates of the vertices, and I tried to use it as boleean but I could'nt write it so that python uderstands it as the area defined by those points.

    Is this problem easy to solve?

    Continue reading...

Compartilhe esta Página