2016-11-09 5 views
2

matplotlib.path.Pathを使用して、点の集合がポリゴンで囲まれた領域内にあるかどうかを確認しています。私のアプローチは、2つのチェックとループを伴う:多角形の穴の中に多角形の穴がある点を指す

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ループを伴わない高速なアプローチがありますか?あなたは、単に行うことができ

答えて

0

-

insideRegion = insideOuter & ~insideInner 
+0

スーパー素敵な、おかげでたくさん!最初の提案はより一般的なケースでは安全です: 'False - True = -1'、' False&〜True = 0'です。 – mtgoncalves

+0

@mtgoncalves良い点!それを削除しました。 – Divakar

関連する問題