package.nested.js Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of point-in-polygon Show documentation
Show all versions of point-in-polygon Show documentation
determine if a point is inside a polygon with a ray intersection counting algorithm
The newest version!
// ray-casting algorithm based on
// https://wrf.ecse.rpi.edu/Research/Short_Notes/pnpoly.html
module.exports = function pointInPolygonNested (point, vs, start, end) {
var x = point[0], y = point[1];
var inside = false;
if (start === undefined) start = 0;
if (end === undefined) end = vs.length;
var len = end - start;
for (var i = 0, j = len - 1; i < len; j = i++) {
var xi = vs[i+start][0], yi = vs[i+start][1];
var xj = vs[j+start][0], yj = vs[j+start][1];
var intersect = ((yi > y) !== (yj > y))
&& (x < (xj - xi) * (y - yi) / (yj - yi) + xi);
if (intersect) inside = !inside;
}
return inside;
};