你可以考虑匀称:
from shapely.geometry import Pointfrom shapely.geometry.polygon import Polygonpoint = Point(0.5, 0.5)polygon = Polygon([(0, 0), (0, 1), (1, 1), (1, 0)])print(polygon.contains(point))
从您提到的方法中,我只使用了第二个
path.contains_points,并且工作正常。无论如何,根据测试所需的精度,我建议创建一个numpy
bool网格,并将多边形内的所有节点都设置为True(否则为False)。如果要对很多点进行测试,则可能会更快(
尽管请注意,这依赖于您在“像素”公差范围内进行测试 ):
from matplotlib import pathimport matplotlib.pyplot as pltimport numpy as npfirst = -3size = (3-first)/100xv,yv = np.meshgrid(np.linspace(-3,3,100),np.linspace(-3,3,100))p = path.Path([(0,0), (0, 1), (1, 1), (1, 0)]) # square with legs length 1 and bottom left corner at the originflags = p.contains_points(np.hstack((xv.flatten()[:,np.newaxis],yv.flatten()[:,np.newaxis])))grid = np.zeros((101,101),dtype='bool')grid[((xv.flatten()-first)/size).astype('int'),((yv.flatten()-first)/size).astype('int')] = flagsxi,yi = np.random.randint(-300,300,100)/100,np.random.randint(-300,300,100)/100vflag = grid[((xi-first)/size).astype('int'),((yi-first)/size).astype('int')]plt.imshow(grid.T,origin='lower',interpolation='nearest',cmap='binary')plt.scatter(((xi-first)/size).astype('int'),((yi-first)/size).astype('int'),c=vflag,cmap='Greens',s=90)plt.show()
,结果是这样的:
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)