Created
January 18, 2015 13:55
-
-
Save wouterds/c4ce8df1092a06698b97 to your computer and use it in GitHub Desktop.
This file contains 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
var pointInPolygon = function(point, polygon) { | |
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