Created
July 24, 2014 23:15
-
-
Save blackymetal/61ae2de98f9d3bf6a754 to your computer and use it in GitHub Desktop.
Testing point in Polygon. Based on http://www.ecse.rpi.edu/~wrf/Research/Short_Notes/pnpoly.html
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
#include <stdio.h> | |
#include <string.h> | |
int pnpoly(int nvert, float *vertx, float *verty, float testx, float testy); | |
float buffx[3]; | |
float buffy[3]; | |
main() | |
{ | |
buffx[0] = 1.0; buffy[0] = 1.0; | |
buffx[1] = 2.0; buffy[1] = 1.0; | |
buffx[2] = 2.0; buffy[2] = 2.0; | |
buffx[3] = 1.0; buffy[3] = 2.0; | |
int result = pnpoly(3, buffx, buffy, 0, 0); | |
printf("%d", result); | |
} | |
int pnpoly(int nvert, float *vertx, float *verty, float testx, float testy) | |
{ | |
int i, j, c = 0; | |
for (i = 0, j = nvert-1; i < nvert; 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