我正在寻找一种使用NumPy/SciPy确定一个特定点是否在给定顶点的多边形内的方法。
我没有在网上找到这样的方法。使用NumPy/SciPy有没有办法实现这个目标?
我正在寻找一种使用NumPy/SciPy确定一个特定点是否在给定顶点的多边形内的方法。
我没有在网上找到这样的方法。使用NumPy/SciPy有没有办法实现这个目标?
你有没有考虑过Shapely?只需创建一个多边形,然后检查多边形是否包含一个点。
>>> from shapely.geometry import Point
>>> from shapely.geometry.polygon import Polygon
>>> point = Point(0.5, 0.5)
>>> polygon = Polygon([(0, 0), (0, 1), (1, 1), (1, 0)])
>>> polygon.contains(point)
True
>>> point2 = Point((10, 10))
>>> polygon.contains(point2)
False
pip installs shapely
:( - user1767754Scikit-image(skimage
)由与SciPy相同的社区维护,并提供了以下方法:https://scikit-image.org/docs/stable/api/skimage.measure.html#skimage.measure.points_in_poly
用法和小例子:
import numpy as np
from skimage.measure import points_in_poly
import matplotlib.pyplot as plt
points = np.array([[0, 0], [1, 0], [1, 1], [0, 1]])
poly = np.array([[0.5, 0.5], [1.5, 0.5], [1.5, 1.5], [0.5, 1.5]])
poly_to_plot = np.vstack([poly, poly[0]])
is_in_poly = points_in_poly(points, poly)
for ii in range(len(points)):
plt.scatter(*points[ii], color="blue" if is_in_poly[ii] else "black")
plt.plot(*poly_to_plot.T, color="red")
plt.show()