Created
October 6, 2012 00:13
-
-
Save SpencerCooley/3843186 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//point in polygon algorithm | |
var pointInPolygon = 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