缠绕数算法未产生预期结果

问题描述 投票:1回答:1

因此,我实现了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
1个回答
0
投票

这两种方法不是相同的标准。

© www.soinside.com 2019 - 2024. All rights reserved.