3

I need algorithm which can tell, if Point lies inside/outside or on boundary (edge) of convex hull (C/C++).

Convex hull is described as array of Points X,Y, integer, connection are from i to i+1.

Currently Im using winding number algorithm, described here: http://geomalgorithms.com/a03-_inclusion.html It's function "wn_PnPoly()".

Is it possible, and how, to make winding number algorithm detect, if Point lies exactly on boundary (edge) of convex? Is there another algorithm to do that? (Needs to work on ints).

user3157855
  • 714
  • 2
  • 9
  • 24
  • Just read the implementation of the function. You will find a test whether a point is left/on/right the edge. –  Jun 08 '16 at 13:16

2 Answers2

8

Found solution:

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;
}
user3157855
  • 714
  • 2
  • 9
  • 24
  • 1
    Is missing the isLeft() function, but it could be found in other sources as well. For example here: http://forums.codeguru.com/showthread.php?497679-To-check-if-a-point-is-inside-a-polygon – st6mm Apr 23 '17 at 13:52
-1

I'm not aware of the Winding number algorithm but to detect whether a point lies on one of the edges, you can just loop through all the edges of the convex hull and do the following check:

If points u, v are consecutive points on the convex hull and p is the point in consideration then whether,

p - u = lambda*(v - u) where lambda is any scalar between 0 and 1.

Abhishek Bansal
  • 12,589
  • 4
  • 31
  • 46