79276139

Date: 2024-12-12 18:10:39
Score: 2
Natty:
Report link

with regards to @pseudocubic answer, I want to add an answer where you can include boundary condition too. the method adds, using shapely >> touches and instead of appending eligible points, excluding ineligible points, and at the end removing all empty points for both dimensions:

import numpy as np
from shapely.geometry import LineString, Polygon, Point

gridX, gridY = np.mgrid[0.0:10.0, 0.0:10.0]
poly = Polygon([[1,1],[1,7],[7,7],[7,1]])
stacked = np.dstack([gridX,gridY])
reshaped = stacked.reshape(100,2)
points = reshaped.tolist()
for i, point in enumerate(points):
        point_geom = Point([point])
        if not poly.contains(point_geom) and not poly.touches(point_geom): ## (x != x_min and x != x_max and y != y_min and y != y_max):  # second condition ensures within boundary
            points[i] = ([],[])  # Mark the point as None if it's outside the polygon

mod_points = [[coord for coord in point if coord] for point in points]  
mod_points = [point for point in mod_points if point is not None and point != []]

And for plotting,

#plot original points
fig = plt.figure()
ax = fig.add_subplot(111)
# Extract the x and y coordinates of polygon
poly_x, poly_y = poly.exterior.xy
ax.plot(poly_x, poly_y, c = "green")
#ploting modified points
ax.scatter(gridX,gridY)
mod_x, mod_y = zip(*mod_points)
ax.scatter(mod_x,mod_y , c = 'red')
plt.show()

points inside polygon

Reasons:
  • Blacklisted phrase (1): regards
  • RegEx Blacklisted phrase (1): I want
  • Long answer (-1):
  • Has code block (-0.5):
  • User mentioned (1): @pseudocubic
  • Low reputation (0.5):
Posted by: hamflow