【发布时间】:2013-11-22 06:59:52
【问题描述】:
使用著名的光线投射算法可以轻松确定一个点是否在凸多边形中。
def point_inside_polygon(x, y, poly):
""" Deciding if a point is inside (True, False otherwise) a polygon,
where poly is a list of pairs (x,y) containing the polygon's vertices.
The algorithm is called the 'Ray Casting Method' """
n = len(poly)
inside = False
p1x, p1y = poly[0]
for i in range(n):
p2x, p2y = poly[i % n]
if y > min(p1y, p2y):
if y <= max(p1y, p2y):
if x <= max(p1x, p2x):
if p1y != p2y:
xinters = (y-p1y) * (p2x-p1x) / (p2y-p1y) + p1x
if p1x == p2x or x <= xinters:
inside = not inside
p1x, p1y = p2x, p2y
return inside
但是如果多边形不是完全凸的呢?
在给定边界点的情况下,我如何确定一个点是否在随机形状多边形中?
假设我有一个边界点的多边形,像这样
我该怎么做?
最好使用 Python,但也欢迎任何通用解决方案。
【问题讨论】:
-
光线投射算法在非凸情况下也不起作用吗?维基链接:en.wikipedia.org/wiki/Point_in_polygon
-
那是你刚刚画的一个非常漂亮的多边形样本,就像毕加索一样。这是算法的帮助:alienryderflex.com/polygon
-
边界点?不是边缘?我们是否应该从嘈杂的图像中插入多边形?你给出的图片无论如何都不是一个多边形。它甚至不是封闭的或连续的。
-
@user2357112 很抱歉造成混乱,您可以关闭打开的部分以将其视为多边形。在我的问题中没那么重要。