Created
October 18, 2021 07:07
-
-
Save zazaulola/25458765f5be70dfa726fb9fc06ed2f8 to your computer and use it in GitHub Desktop.
check the coordinates of a point in that it is inside a polygon
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
function inside(point, vs) { | |
// ray-casting algorithm based on | |
// https://wrf.ecse.rpi.edu/Research/Short_Notes/pnpoly.html/pnpoly.html | |
// https://stackoverflow.com/questions/22521982/check-if-point-is-inside-a-polygon/29915728#29915728 | |
// https://github.com/substack/point-in-polygon | |
var x = point[0], y = point[1]; | |
var inside = false; | |
for (var i = 0, j = vs.length - 1; i < vs.length; j = i++) { | |
var xi = vs[i][0], yi = vs[i][1]; | |
var xj = vs[j][0], yj = vs[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