I have an algorithm that can find if a point is inside a polygon.
int CGlEngineFunctions::PointInPoly(int npts, float *xp, float *yp, float x, float y)
{
int i, j, c = 0;
for (i = 0, j = npts-1; i < npts; j = i++) {
if ((((yp[i] <= y) && (y < yp[j])) ||
((yp[j] <= y) && (y < yp[i]))) &&
(x < (xp[j] - xp[i]) * (y - yp[i]) / (yp[j] - yp[i]) + xp[i]))
c = !c;
}
return c;
}
My only issue with it is it assumes an odd winding rule. What I mean by this is that if the polygon is self intersecting, certain parts that it would considered to be ’empty’ will return as false. What I’d need in even if it self intersects, anything inside the polygon will return true.
Thanks
Beware: this answer is wrong. I have no time to fix it right now, but see the comments.
This casts a ray from the point to infinity, and checks for intersections with each of the polygon’s edges. Each time an intersection is found, the flag
cis toggled:So an even number of intersections means an even number of toggles, so
cwill be 0 at the end. An odd number of intersections means an odd number of toggles, socwill be 1.What you want instead is to set the
cflag if any intersection occurs:And for good measure, you can then eliminate
centirely, and terminate early: