Skip to content

Instantly share code, notes, and snippets.

@Sigmus
Created December 17, 2012 13:42
Show Gist options
  • Save Sigmus/4318354 to your computer and use it in GitHub Desktop.
Save Sigmus/4318354 to your computer and use it in GitHub Desktop.
Asserts if a given point is inside a polygon.
//+
//@ http://jsfromhell.com/math/is-point-in-poly [v1.0]
function isPointInPoly(poly, pt){
for(var c = false, i = -1, l = poly.length, j = l - 1; ++i < l; j = i)
((poly[i].y <= pt.y && pt.y < poly[j].y) || (poly[j].y <= pt.y && pt.y < poly[i].y))
&& (pt.x < (poly[j].x - poly[i].x) * (pt.y - poly[i].y) / (poly[j].y - poly[i].y) + poly[i].x)
&& (c = !c);
return c;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment