I have some coordinates and i want to create some random coordinates that are inside the polygon.
coords = np.random.rand(20, 2)* 2
I have created random coordinates, but they are outside my polygon.
poly= Polygon([(22.794525711443953, 39.431753895579845), (22.797156635193346,39.43552620818886), (22.79643512096834,39.4363589771401), (22.79243347988472,39.43454099778662), (22.794525711443953, 39.431753895579845)])
def point_inside_polygon(x,y,poly):
n = len(poly)
inside =False
p1x,p1y = poly[0]
for i in range(n+1):
p2x,p2y = poly[i % n]
if y > min(p1y,p2y):
if y <= max(p1y,p2y):
if x <= max(p1x,p2x):
if p1y != p2y:
xinters = (y-p1y)*(p2x-p1x)/(p2y-p1y)+p1x
if p1x == p2x or x <= xinters:
inside = not inside
p1x,p1y = p2x,p2y
return inside
coords = np.random.rand(20, 2)* 2
print(coords)