Skip to content

Instantly share code, notes, and snippets.

@BichengLUO
Created September 21, 2015 11:42
Show Gist options
  • Save BichengLUO/6a8a518c2c26c6cfab51 to your computer and use it in GitHub Desktop.
Save BichengLUO/6a8a518c2c26c6cfab51 to your computer and use it in GitHub Desktop.
In-polygon test
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