Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/364.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 具有多边形孔的多边形区域内的点_Python_Numpy_Matplotlib_Vectorization_Point In Polygon - Fatal编程技术网

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