Skip to content

Instantly share code, notes, and snippets.

@schell
Created January 12, 2011 18:44
Show Gist options
  • Save schell/776634 to your computer and use it in GitHub Desktop.
Save schell/776634 to your computer and use it in GitHub Desktop.
Determine whether a point lies within a polygon
//The following code is by Randolph Franklin, it returns 1 for interior points and 0 for exterior points.
int pnpoly(int npol, float *xp, float *yp, float x, float y)
{
int i, j, c = 0;
for (i = 0, j = npol-1; i < npol; 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;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment