Forked from vlasky/point_in_polygon_using_winding_number.js
Created
February 25, 2022 00:54
-
-
Save meta-hub/289028828d5a93d4adbfb6b45bd6f060 to your computer and use it in GitHub Desktop.
JavaScript implementation of winding number algorithm to determine whether a point 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
//JavaScript implementation of winding number algorithm to determine whether a point is inside a polygon | |
//Based on C++ implementation of wn_PnPoly() published on http://geomalgorithms.com/a03-_inclusion.html | |
function pointInPolygon(point, vs) { | |
const x = point[0], y = point[1]; | |
let wn = 0; | |
for (let i = 0, j = vs.length - 1; i < vs.length; j = i++) { | |
let xi = vs[i][0], yi = vs[i][1]; | |
let xj = vs[j][0], yj = vs[j][1]; | |
if (yj <= y) { | |
if (yi > y) { | |
if (isLeft([xj, yj], [xi, yi], [x,y]) > 0) { | |
wn++; | |
} | |
} | |
} else { | |
if (yi <= y) { | |
if (isLeft([xj, yj], [xi, yi], [x, y]) < 0) { | |
wn--; | |
} | |
} | |
} | |
} | |
return wn != 0; | |
}; | |
function isLeft(P0, P1, P2) { | |
let res = ( (P1[0] - P0[0]) * (P2[1] - P0[1]) | |
- (P2[0] - P0[0]) * (P1[1] - P0[1]) ); | |
return res; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment