gpt4 book ai didi

python - 使用 matplotlib 求解多边形中的点

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:59:18 26 4
gpt4 key购买 nike

我正在寻找一种算法来检查一个点是否在多边形内。

我目前正在使用 mplPath 和 contains_point() 但它在某些情况下似乎不起作用。

2016 年 9 月 16 日编辑:

好的,所以我通过简单地检查该点是否也在边缘来改进我的代码。不过,对于矩形和领结示例,我仍然存在一些问题:

新代码:

#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])

新代码的输出:

enter image description here

旧代码:

#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()

我想出了以下 graph .如您所见,红色包围的三个点被指示为在多边形(蓝色)之外,而我希望它们在多边形内部。

我还尝试更改路径 bbPath.contains_points(points, radius = 1.) 的半径值,但这没有任何区别。

欢迎任何帮助。

编辑:

enter image description here这个问题的答案中提出的算法的屏幕截图似乎表明它在其他情况下失败。

最佳答案

好吧,我终于设法改用 shapely 来完成它。

#for PIP problem
import matplotlib.path as mplPath
import numpy as np
#for plot
import matplotlib.pyplot as plt
import shapely.geometry as shapely

class MyPoly(shapely.Polygon):
def __init__(self,points):
super(MyPoly,self).__init__(points)
self.points = points
self.points_shapely = [shapely.Point(p[0],p[1]) for p in points]

def convert_to_shapely_points_and_poly(poly,points):
poly_shapely = MyPoly(poly)
points_shapely = [shapely.Point(p[0],p[1]) for p in points]
return poly_shapely,points_shapely

def plot(poly_init,points_init):
#convert to shapely poly and points
poly,points = convert_to_shapely_points_and_poly(poly_init,points_init)

#plot polygon
plt.plot(*zip(*poly.points))

#plot points
xs,ys,cs = [],[],[]
for point in points:
xs.append(point.x)
ys.append(point.y)
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.y - a.y) * (b.x - a.x) - (c.x - a.x) * (b.y - a.y)
if abs(crossproduct) > 0.01 : return False # (or != 0 if using integers)

dotproduct = (c.x - a.x) * (b.x - a.x) + (c.y - a.y)*(b.y - a.y)
if dotproduct < 0 : return False

squaredlengthba = (b.x - a.x)*(b.x - a.x) + (b.y - a.y)*(b.y - a.y)
if dotproduct > squaredlengthba: return False

return True

def get_edges(poly):
# get edges
edges = []
for i in range(len(poly.points)-1):
t = [poly.points_shapely[i],poly.points_shapely[i+1]]
edges.append(t)
return edges


def inPoly(poly,point):
if poly.contains(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 clockwise
5 : [[0,0],[20,0],[20,10],[0,10],[0,0]] # test rect counter-clockwise
}

#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,2],[30,8]], # rect shape test pts
5 : [[0,0],[0,10],[20,0],[20,10],[10,0],[10,5],[15,2],[30,8]] # 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])

关于python - 使用 matplotlib 求解多边形中的点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39487194/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com