Skip to content

Instantly share code, notes, and snippets.

@SpencerCooley
Created October 6, 2012 00:13
Show Gist options
  • Save SpencerCooley/3843188 to your computer and use it in GitHub Desktop.
Save SpencerCooley/3843188 to your computer and use it in GitHub Desktop.
//point in polygon algorithm
var inside = function (point, polygon) {
// ray-casting algorithm based on
// http://www.ecse.rpi.edu/Homepages/wrf/Research/Short_Notes/pnpoly.html
var x = point[0], y = point[1];
var inside = false;
for (var i = 0, j = polygon.length - 1; i < polygon.length; j = i++) {
var xi = polygon[i][0], yi = polygon[i][1];
var xj = polygon[j][0], yj = polygon[j][1];
var intersect = ((yi > y) != (yj > y))
&& (x < (xj - xi) * (y - yi) / (yj - yi) + xi);
if (intersect) inside = !inside;
}
return inside;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment