point-in-polygon-hao
Advanced tools
Comparing version 1.1.0 to 1.2.0
@@ -0,1 +1,5 @@ | ||
# 1.2.0 | ||
- Use robust-predicates library for some calcs | ||
- Refactor logic to reduce package size and improve performance | ||
- Move to vitest | ||
@@ -2,0 +6,0 @@ # 1.1.0 |
(function (global, factory) { | ||
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() : | ||
typeof define === 'function' && define.amd ? define(factory) : | ||
(global = global || self, global.pointInPolygon = factory()); | ||
}(this, (function () { 'use strict'; | ||
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory(require('robust-predicates')) : | ||
typeof define === 'function' && define.amd ? define(['robust-predicates'], factory) : | ||
(global = global || self, global.pointInPolygon = factory(global.robustPredicates)); | ||
}(this, (function (robustPredicates) { 'use strict'; | ||
function pointInPolygon(p, polygon) { | ||
var i = 0; | ||
var ii = 0; | ||
var i; | ||
var ii; | ||
var k = 0; | ||
var f = 0; | ||
var u1 = 0; | ||
var v1 = 0; | ||
var u2 = 0; | ||
var v2 = 0; | ||
var currentP = null; | ||
var nextP = null; | ||
var f; | ||
var u1; | ||
var v1; | ||
var u2; | ||
var v2; | ||
var currentP; | ||
var nextP; | ||
@@ -23,6 +23,6 @@ var x = p[0]; | ||
var numContours = polygon.length; | ||
for (i; i < numContours; i++) { | ||
for (i = 0; i < numContours; i++) { | ||
ii = 0; | ||
var contourLen = polygon[i].length - 1; | ||
var contour = polygon[i]; | ||
var contourLen = contour.length - 1; | ||
@@ -41,33 +41,11 @@ currentP = contour[0]; | ||
u2 = nextP[0] - x; | ||
v2 = nextP[1] - y; | ||
if ((v1 < 0 && v2 < 0) || (v1 > 0 && v2 > 0)) { | ||
currentP = nextP; | ||
v1 = v2; | ||
u1 = currentP[0] - x; | ||
continue | ||
} | ||
u2 = nextP[0] - p[0]; | ||
if (v2 > 0 && v1 <= 0) { | ||
f = (u1 * v2) - (u2 * v1); | ||
if (f > 0) { k = k + 1; } | ||
else if (f === 0) { return 0 } | ||
} else if (v1 > 0 && v2 <= 0) { | ||
f = (u1 * v2) - (u2 * v1); | ||
if (f < 0) { k = k + 1; } | ||
else if (f === 0) { return 0 } | ||
} else if (v2 === 0 && v1 < 0) { | ||
f = (u1 * v2) - (u2 * v1); | ||
if (v1 === 0 && v2 === 0) { | ||
if ((u2 <= 0 && u1 >= 0) || (u1 <= 0 && u2 >= 0)) { return 0 } | ||
} else if ((v2 >= 0 && v1 < 0) || (v2 < 0 && v1 >= 0)) { | ||
f = robustPredicates.orient2d(u1, u2, v1, v2, 0, 0); | ||
if (f === 0) { return 0 } | ||
} else if (v1 === 0 && v2 < 0) { | ||
f = u1 * v2 - u2 * v1; | ||
if (f === 0) { return 0 } | ||
} else if (v1 === 0 && v2 === 0) { | ||
if (u2 <= 0 && u1 >= 0) { | ||
return 0 | ||
} else if (u1 <= 0 && u2 >= 0) { | ||
return 0 | ||
} | ||
if ((f > 0 && v2 > 0 && v1 <= 0) || (f < 0 && v2 <= 0 && v1 > 0)) { k++; } | ||
} | ||
@@ -74,0 +52,0 @@ currentP = nextP; |
@@ -1,1 +0,1 @@ | ||
!function(e,n){"object"==typeof exports&&"undefined"!=typeof module?module.exports=n():"function"==typeof define&&define.amd?define(n):(e=e||self).pointInPolygon=n()}(this,function(){"use strict";return function(e,n){for(var i=0,r=0,t=0,f=0,o=0,u=0,l=0,s=0,d=null,a=null,c=e[0],p=e[1],h=n.length;i<h;i++){r=0;var m=n[i].length-1,g=n[i];if((d=g[0])[0]!==g[m][0]&&d[1]!==g[m][1])throw new Error("First and last coordinates in a ring must be the same");for(o=d[0]-c,u=d[1]-p;r<m;r++)if(s=(a=g[r+1])[1]-p,u<0&&s<0||u>0&&s>0)u=s,o=(d=a)[0]-c;else{if(l=a[0]-e[0],s>0&&u<=0){if((f=o*s-l*u)>0)t+=1;else if(0===f)return 0}else if(u>0&&s<=0){if((f=o*s-l*u)<0)t+=1;else if(0===f)return 0}else if(0===s&&u<0){if(0==(f=o*s-l*u))return 0}else if(0===u&&s<0){if(0==(f=o*s-l*u))return 0}else if(0===u&&0===s){if(l<=0&&o>=0)return 0;if(o<=0&&l>=0)return 0}d=a,u=s,o=l}}return t%2!=0}}); | ||
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t(require("robust-predicates")):"function"==typeof define&&define.amd?define(["robust-predicates"],t):(e=e||self).pointInPolygon=t(e.robustPredicates)}(this,function(e){"use strict";return function(t,r){var n,i,o,f,s,u,d,a,c,l=0,p=t[0],b=t[1],h=r.length;for(n=0;n<h;n++){i=0;var m=r[n],g=m.length-1;if((a=m[0])[0]!==m[g][0]&&a[1]!==m[g][1])throw new Error("First and last coordinates in a ring must be the same");for(f=a[0]-p,s=a[1]-b;i<g;i++){if(u=(c=m[i+1])[0]-p,d=c[1]-b,0===s&&0===d){if(u<=0&&f>=0||f<=0&&u>=0)return 0}else if(d>=0&&s<0||d<0&&s>=0){if(0===(o=e.orient2d(f,u,s,d,0,0)))return 0;(o>0&&d>0&&s<=0||o<0&&d<=0&&s>0)&&l++}a=c,s=d,f=u}}return l%2!=0}}); |
{ | ||
"name": "point-in-polygon-hao", | ||
"version": "1.1.0", | ||
"version": "1.2.0", | ||
"type": "module", | ||
"description": "A point in polygon based on the paper Optimal Reliable Point-in-Polygon Test and Differential Coding Boolean Operations on Polygons", | ||
"main": "dist/pointInPolygon.js", | ||
"main": "dist/pointInPolygon.mjs", | ||
"module": "dist/pointInPolygon.mjs", | ||
@@ -13,9 +14,9 @@ "unpkg": "dist/pointInPolygon.min.js", | ||
"build": "rollup -c", | ||
"test": "ava" | ||
"test": "vitest" | ||
}, | ||
"dependencies": {}, | ||
"dependencies": { | ||
"robust-predicates": "^3.0.2" | ||
}, | ||
"devDependencies": { | ||
"@rollup/plugin-buble": "^0.21.3", | ||
"@turf/boolean-point-in-polygon": "^6.0.1", | ||
"ava": "^3.15.0", | ||
"benchmark": "^2.1.4", | ||
@@ -29,13 +30,5 @@ "eslint": "^5.15.3", | ||
"rollup": "^1.7.0", | ||
"rollup-plugin-terser": "^4.0.4" | ||
"rollup-plugin-terser": "^4.0.4", | ||
"vitest": "^2.1.6" | ||
}, | ||
"ava": { | ||
"files": [ | ||
"test/*.spec.js" | ||
], | ||
"require": [ | ||
"esm" | ||
], | ||
"verbose": true | ||
}, | ||
"keywords": [ | ||
@@ -42,0 +35,0 @@ "point-in-polygon", |
@@ -55,5 +55,5 @@ A small library for detecting in a point lies inside a polygon | ||
// For a point in a much larger geometry (700+ vertices) | ||
point-in-poly-hao x 474,180 ops/sec ±0.55% (93 runs sampled) | ||
point-in-polygon x 489,649 ops/sec ±0.75% (91 runs sampled) | ||
robust-point-in-polygon x 376,268 ops/sec ±0.79% (89 runs sampled) | ||
point-in-poly-hao x 381,184 ops/sec ±0.80% (87 runs sampled) | ||
point-in-polygon x 285,734 ops/sec ±1.33% (91 runs sampled) | ||
robust-point-in-polygon x 267,738 ops/sec ±0.78% (93 runs sampled) | ||
```` | ||
@@ -63,5 +63,5 @@ | ||
// For a point in bounding box check | ||
point-in-poly-hao x 29,365,704 ops/sec ±1.30% (90 runs sampled) | ||
point-in-polygon x 42,339,450 ops/sec ±0.78% (95 runs sampled) | ||
robust-point-in-polygon x 20,675,569 ops/sec ±0.65% (95 runs sampled) | ||
point-in-poly-hao x 25,822,227 ops/sec ±2.87% (86 runs sampled) | ||
point-in-polygon x 30,321,920 ops/sec ±2.14% (91 runs sampled) | ||
robust-point-in-polygon x 26,708,560 ops/sec ±1.13% (91 runs sampled) | ||
```` | ||
@@ -75,2 +75,3 @@ | ||
* Works irrespective of winding order of polygon | ||
* Does not appear to be effected by floating point errors compared to `point-in-polygon` or `robust-point-in-polygon` | ||
* ~~Does not appear to be effected by floating point errors compared to `point-in-polygon` or `robust-point-in-polygon`~~ | ||
* Added robust-predicates to deal with some floating point errors |
@@ -0,21 +1,22 @@ | ||
import {orient2d} from 'robust-predicates' | ||
export default function pointInPolygon(p, polygon) { | ||
let i = 0 | ||
let ii = 0 | ||
let i | ||
let ii | ||
let k = 0 | ||
let f = 0 | ||
let u1 = 0 | ||
let v1 = 0 | ||
let u2 = 0 | ||
let v2 = 0 | ||
let currentP = null | ||
let nextP = null | ||
let f | ||
let u1 | ||
let v1 | ||
let u2 | ||
let v2 | ||
let currentP | ||
let nextP | ||
const x = p[0] | ||
const y = p[1] | ||
const [x, y] = p | ||
const numContours = polygon.length | ||
for (i; i < numContours; i++) { | ||
for (i = 0; i < numContours; i++) { | ||
ii = 0 | ||
const contourLen = polygon[i].length - 1 | ||
const contour = polygon[i] | ||
const contourLen = contour.length - 1 | ||
@@ -34,33 +35,11 @@ currentP = contour[0] | ||
u2 = nextP[0] - x | ||
v2 = nextP[1] - y | ||
if ((v1 < 0 && v2 < 0) || (v1 > 0 && v2 > 0)) { | ||
currentP = nextP | ||
v1 = v2 | ||
u1 = currentP[0] - x | ||
continue | ||
} | ||
u2 = nextP[0] - p[0] | ||
if (v2 > 0 && v1 <= 0) { | ||
f = (u1 * v2) - (u2 * v1) | ||
if (f > 0) k = k + 1 | ||
else if (f === 0) return 0 | ||
} else if (v1 > 0 && v2 <= 0) { | ||
f = (u1 * v2) - (u2 * v1) | ||
if (f < 0) k = k + 1 | ||
else if (f === 0) return 0 | ||
} else if (v2 === 0 && v1 < 0) { | ||
f = (u1 * v2) - (u2 * v1) | ||
if (v1 === 0 && v2 === 0) { | ||
if ((u2 <= 0 && u1 >= 0) || (u1 <= 0 && u2 >= 0)) return 0 | ||
} else if ((v2 >= 0 && v1 < 0) || (v2 < 0 && v1 >= 0)) { | ||
f = orient2d(u1, u2, v1, v2, 0, 0) | ||
if (f === 0) return 0 | ||
} else if (v1 === 0 && v2 < 0) { | ||
f = u1 * v2 - u2 * v1 | ||
if (f === 0) return 0 | ||
} else if (v1 === 0 && v2 === 0) { | ||
if (u2 <= 0 && u1 >= 0) { | ||
return 0 | ||
} else if (u1 <= 0 && u2 >= 0) { | ||
return 0 | ||
} | ||
if ((f > 0 && v2 > 0 && v1 <= 0) || (f < 0 && v2 <= 0 && v1 > 0)) k++ | ||
} | ||
@@ -67,0 +46,0 @@ currentP = nextP |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Minified code
QualityThis package contains minified code. This may be harmless in some cases where minified code is included in packaged libraries, however packages on npm should not minify code.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
11
75
Yes
11476
1
171
1
+ Addedrobust-predicates@^3.0.2
+ Addedrobust-predicates@3.0.2(transitive)