포인트가 다각형 내에 있는지 확인하는 알고리즘을 찾고 있습니다.Matplotlib을 사용하여 다각형 내의 점을 푸시
현재 mplPath 및 contains_point()를 사용하고 있지만 경우에 따라 작동하지 않는 것 같습니다.
편집 2016년 9월 16일는 :
좋아요 그래서 단순히 곳도 가장자리에 포인트 경우 확인하여 내 코드를 imporved. 난 여전히 사각형 몇 가지 문제를 가지고 있지만 예를 들어 타이 나비 :
NEW CODE :
#for PIP problem
import matplotlib.path as mplPath
import numpy as np
#for plot
import matplotlib.pyplot as plt
def plot(poly,points):
bbPath = mplPath.Path(poly)
#plot polygon
plt.plot(*zip(*poly))
#plot points
xs,ys,cs = [],[],[]
for point in points:
xs.append(point[0])
ys.append(point[1])
color = inPoly(poly,point)
cs.append(color)
print point,":", color
plt.scatter(xs,ys, c = cs , s = 20*4*2)
#setting limits
axes = plt.gca()
axes.set_xlim([min(xs)-5,max(xs)+50])
axes.set_ylim([min(ys)-5,max(ys)+10])
plt.show()
def isBetween(a, b, c): #is c between a and b ?
crossproduct = (c[1] - a[1]) * (b[0] - a[0]) - (c[0] - a[0]) * (b[1] - a[1])
if abs(crossproduct) > 0.01 : return False # (or != 0 if using integers)
dotproduct = (c[0] - a[0]) * (b[0] - a[0]) + (c[1] - a[1])*(b[1] - a[1])
if dotproduct < 0 : return False
squaredlengthba = (b[0] - a[0])*(b[0] - a[0]) + (b[1] - a[1])*(b[1] - a[1])
if dotproduct > squaredlengthba: return False
return True
def get_edges(poly):
# get edges
edges = []
for i in range(len(poly)-1):
t = [poly[i],poly[i+1]]
edges.append(t)
return edges
def inPoly(poly,point):
if bbPath.contains_point(point) == True:
return 1
else:
for e in get_edges(poly):
if isBetween(e[0],e[1],point):
return 1
return 0
# TESTS ========================================================================
#set up poly
polys = {
1 : [[10,10],[10,50],[50,50],[50,80],[100,80],[100,10],[10,10]], # test rectangulary shape
2 : [[20,10],[10,20],[30,20],[20,10]], # test triangle
3 : [[0,0],[0,10],[20,0],[20,10],[0,0]], # test bow-tie
4 : [[0,0],[0,10],[20,10],[20,0],[0,0]] # test rect
}
#points to check
points = {
1 : [(10,25),(50,75),(60,10),(20,20),(20,60),(40,50)], # rectangulary shape test pts
2 : [[20,10],[10,20],[30,20],[-5,0],[20,15]] , # triangle test pts
3 : [[0,0],[0,10],[20,0],[20,10],[10,0],[10,5],[15,5]], # bow-tie shape test pts
4 : [[0,0],[0,10],[20,0],[20,10],[10,0],[10,5],[15,5]] # rect shape test pts
}
#print bbPath.contains_points(points) #0 if outside, 1 if inside
for data in zip(polys.itervalues(),points.itervalues()):
plot(data[0],data[1])
출력 새로운 코드에서 :
OLD CODE :
#for PIP problem
import matplotlib.path as mplPath
import numpy as np
#for plot
import matplotlib.pyplot as plt
#set up poly
array = np.array([[10,10],[10,50],[50,50],[50,80],[100,80],[100,10]])
bbPath = mplPath.Path(array)
#points to check
points = [(10,25),(50,75),(60,10),(20,20),(20,60),(40,50)]
print bbPath.contains_points(points) #0 if outside, 1 if inside
#plot polygon
plt.plot(*zip(*array))
#plot points
xs,ys,cs = [],[],[]
for point in points:
xs.append(point[0])
ys.append(point[1])
cs.append(bbPath.contains_point(point))
plt.scatter(xs,ys, c = cs)
#setting limits
axes = plt.gca()
axes.set_xlim([0,120])
axes.set_ylim([0,100])
plt.show()
나는 다음을 생각해 낸다. . 당신이 볼 수 있듯이, 붉은 색으로 둘러싸인 세 점은 그들이 안에있을 것으로 기대할 때 (파란색으로) 다각형 바깥에있는 것으로 표시됩니다.
나는 또한 경로 bbPath.contains_points(points, radius = 1.)
의 반지름 값을 변경해 보았지만 아무런 차이가 없었습니다.
도움이 될 것입니다.
는편집 :이 질문에 대한 대답에서 제안 된 알고리즘에서
안녕하세요. 시도해 주셔서 감사합니다. 그러나 나는 그것에 대한 몇 가지 테스트를 더 실시했고 대부분 폴리곤 가장자리에 정확히 위치하지 않는 폼 포인트를 실패했습니다. 여기에 이미지를 추가 할 수 없기 때문에 스크린 샷으로 내 질문을 편집했습니다. – Sorade