卷组数算法和凸边界/边缘上的点

Winding number algorithm and point on boundary/edge of convex

本文关键字:边缘 边界 算法      更新时间:2023-10-16

我需要算法来判断点是位于凸包(C/C++)的内部/外部还是边界(边缘)。

凸包被描述为点 X,Y、整数、连接从 i 到 i+1 的数组。

目前我使用的是绕组数算法,描述如下:http://geomalgorithms.com/a03-_inclusion.html它是函数"wn_PnPoly()"。

是否有可能以及如何使缠绕数算法检测,如果点正好位于凸的边界(边缘)上?有没有另一种算法可以做到这一点?(需要在整数上工作)。

找到的解决方案:

int wn_PnPoly2(Point P, vector<Point> V, int n)
{
    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
            {
                int l = isLeft(V[i], V[i + 1], P);
                if (l > 0)  // P left of  edge
                    ++wn;            // have  a valid up intersect
                else if (l == 0) // boundary
                    return 0;
            }
        }
        else {                        // start y > P.y (no test needed)
            if (V[i + 1].Y <= P.Y)     // a downward crossing
            {
                int l = isLeft(V[i], V[i + 1], P);
                if (l < 0)  // P right of  edge
                    --wn;            // have  a valid down intersect
                else if (l == 0)
                    return 0;
            }
        }
    }
    return wn;
}
我不知道

缠绕数算法,但要检测点是否位于其中一条边上,您可以循环穿过凸包的所有边缘并进行以下检查:

如果点 u、v 是凸包上的连续点,p 是要考虑的点,那么,

p - u = lambda*(v - u)其中lambda是 0 到 1 之间的任何标量。