Skip to content

Instantly share code, notes, and snippets.

@joezuntz
Created January 22, 2013 14:08
Show Gist options
  • Save joezuntz/4594899 to your computer and use it in GitHub Desktop.
Save joezuntz/4594899 to your computer and use it in GitHub Desktop.
C function to check if a point is in an arbitrary polygon. I can't vouch for this or even recall where I got it!
int point_in_polygon(int n, float *vertx, float *verty, float testx, float testy)
{
int i, j, c = 0;
for (i = 0, j=n-1; i < n; j = i++) {
if ( ((verty[i]>testy) != (verty[j]>testy)) &&
(testx < (vertx[j]-vertx[i]) * (testy-verty[i]) / (verty[j]-verty[i]) + vertx[i]) )
c = !c;
}
return c;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment