Created
January 22, 2013 14:08
-
-
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!
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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