Точка внутри многоугольной области с многоугольным отверстием
Я использую 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?
1 ответ
Решение
Вы могли бы просто сделать -
insideRegion = insideOuter & ~insideInner