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 루프가 포함되지 않은 더 빠른 방법이 있습니까?
슈퍼 좋은, 덕분에 많이! 첫 번째 제안은보다 일반적인 경우에 더 안전하다고 생각합니다 : False - True = -1' False & True = 0'. – mtgoncalves
@mtgoncalves 좋은 지적! 그걸 없앴습니다. – Divakar