【发布时间】:2020-08-10 20:14:47
【问题描述】:
因此,我实现了在 http://geomalgorithms.com/a03-_inclusion.html 上找到的绕组数和交叉数算法的一个非常未优化的版本,但是我遇到了一个绕组数算法无法产生预期结果的情况。
我创建了一个多边形和三个点,如图here 所示。对于 P0 和 P2,两种算法的行为都是可预测的。但是,对于点 P1(多边形边界内的“零空间”包含的点),交叉数算法成功,而绕组数算法无法识别该点不包含在多边形中。
这是实现的算法:
int wn_PnPoly(Vector2 P, Vector2[] V)
{
int n = V.Length - 1;
int wn = 0; // the winding number counter
// loop through all edges of the polygon
for (int i = 0; i < n; i++)
{ // edge from V[i] to V[i+1]
if (V[i].y <= P.y)
{ // start y <= P.y
if (V[i + 1].y > P.y) // an upward crossing
if (isLeft(V[i], V[i + 1], P) > 0) // P left of edge
++wn; // have a valid up intersect
}
else
{ // start y > P.y (no test needed)
if (V[i + 1].y <= P.y) // a downward crossing
if (isLeft(V[i], V[i + 1], P) < 0) // P right of edge
--wn; // have a valid down intersect
}
}
return wn;
}
float isLeft(Vector2 P0, Vector2 P1, Vector2 P2)
{
return ((P1.x - P0.x) * (P2.y - P0.y)
- (P2.x - P0.x) * (P1.y - P0.y));
}
我在这里遗漏了什么明显的东西吗?为什么在这种情况下交叉数算法成功而绕组数失败?
【问题讨论】:
标签: algorithm polygon point-in-polygon