Python 具有多边形孔的多边形区域内的点
我正在使用Python 具有多边形孔的多边形区域内的点,python,numpy,matplotlib,vectorization,point-in-polygon,Python,Numpy,Matplotlib,Vectorization,Point In Polygon,我正在使用matplotlib.path.path检查一组点是否位于由多边形包围的区域内(带有多边形孔的多边形区域)。我的方法包括两个检查和一个循环: import numpy as np from matplotlib import path # Define coordinates of the boundaries xyOuter = np.array([[-5, -5], [5, -5], [5, 5], [-5, 5]]) xyInner = np.array([[-2, -2],
matplotlib.path.path
检查一组点是否位于由多边形包围的区域内(带有多边形孔的多边形区域)。我的方法包括两个检查和一个循环:
import numpy as np
from matplotlib import path
# Define coordinates of the boundaries
xyOuter = np.array([[-5, -5], [5, -5], [5, 5], [-5, 5]])
xyInner = np.array([[-2, -2], [2, -2], [2, 2], [-2, 2]])
# Convert boundary coordinates to Path objects
xyOuter = path.Path(xyOuter)
xyInner = path.Path(xyInner)
# Define coordinates of the test points
xyPoints = np.linspace(-7, 7, 57)
xyPoints = np.vstack([xyPoints, xyPoints]).T
# Test whether points are inside the outer region
insideOuter = xyOuter.contains_points(xyPoints)
# Test whether points are inside the inner region
insideInner = xyInner.contains_points(xyPoints)
# Initialise boolean array for region bounded by two polygons
insideRegion = np.zeros(insideOuter.shape, dtype=bool)
# Flip False to True if point is inside the outer region AND outside the inner region
for i in range(len(insideRegion)):
if insideOuter[i] == True:
if insideInner[i] == False:
insideRegion[i] = True
# Print results
for o, i, r in zip(insideOuter, insideInner, insideRegion):
print o, i, r
有没有一种不涉及for循环的更快的方法?您可以简单地这样做-
insideRegion = insideOuter & ~insideInner
非常好,非常感谢!我认为第一个建议对于更一般的情况更安全:
False-True=-1
但是False&~True=0
。