Created
September 21, 2015 11:42
-
-
Save BichengLUO/6a8a518c2c26c6cfab51 to your computer and use it in GitHub Desktop.
In-polygon test
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
bool in_polygon(const point &p, point ps[], int len) | |
{ | |
int i, j = len - 1; | |
bool oddNodes = false; | |
for (i = 0; i < len; i++) { | |
if ((ps[i].y < p.y && ps[j].y >= p.y | |
|| ps[j].y < p.y && ps[i].y >= p.y) | |
&& (ps[i].x <= p.x || ps[j].x <= p.x)) | |
{ | |
oddNodes ^= (ps[i].x + (p.y - ps[i].y) / (ps[j].y - ps[i].y) * (ps[j].x - ps[i].x) < p.x); | |
} | |
j = i; | |
} | |
return oddNodes; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment