Skip to content

Instantly share code, notes, and snippets.

@hiroyuki
Created October 3, 2012 15:34
Show Gist options
  • Save hiroyuki/3827613 to your computer and use it in GitHub Desktop.
Save hiroyuki/3827613 to your computer and use it in GitHub Desktop.
Point inside the polygon
//http://alienryderflex.com/polygon/
bool tricks::math::pointInsidePolygon(ofVec2f &p, const vector<ofVec2f> &polygon, int resolution){
int counter = 0;
int i;
double xinters;
ofVec2f p1,p2;
float x = p.x;
float y = p.y;
int N = polygon.size();
p1 = polygon[0];
for (i=1;i<=N;i+=resolution) {
p2 = polygon[i % N];
if (y > MIN(p1.y,p2.y)) {
if (y <= MAX(p1.y,p2.y)) {
if (x <= MAX(p1.x,p2.x)) {
if (p1.y != p2.y) {
xinters = (y-p1.y)*(p2.x-p1.x)/(p2.y-p1.y)+p1.x;
if (p1.x == p2.x || x <= xinters)
counter++;
}
}
}
}
p1 = p2;
}
if (counter % 2 == 0) return false;
else return true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment