Skip to content

Instantly share code, notes, and snippets.

@max-mapper
Created September 11, 2010 05:34
Show Gist options
  • Save max-mapper/574870 to your computer and use it in GitHub Desktop.
Save max-mapper/574870 to your computer and use it in GitHub Desktop.
function isPointInPoly(poly, pt) {
for(var c = false, i = -1, l = poly.length, j = l - 1; ++i < l; j = i)
((poly[i][1] <= pt.y && pt.y < poly[j][1]) || (poly[j][1] <= pt.y && pt.y < poly[i][1])) && (pt.x < (poly[j][0] - poly[i][0]) * (pt.y - poly[i][1]) / (poly[j][1] - poly[i][1]) + poly[i][0]) && (c = !c);
return c;
}
isPointInPoly([[1, 1], [5, 1], [5, 5], [1, 5]], {x: 2, y: 2}); //true
isPointInPoly([[1, 1], [5, 1], [5, 5], [1, 5]], {x: 100, y: 100}); //false
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment