point-in-polygon-hao
Advanced tools
Comparing version 0.0.7 to 1.0.0
@@ -5,3 +5,3 @@ (function (global, factory) { | ||
(global = global || self, global.pointInPolygon = factory()); | ||
}(this, function () { 'use strict'; | ||
}(this, (function () { 'use strict'; | ||
@@ -30,2 +30,7 @@ function pointInPolygon(p, polygon) { | ||
currentP = contour[0]; | ||
if (currentP[0] !== contour[contourLen][0] && | ||
currentP[1] !== contour[contourLen][1]) { | ||
throw new Error('First and last coordinates in a ring must be the same') | ||
} | ||
u1 = currentP[0] - x; | ||
@@ -81,2 +86,2 @@ v1 = currentP[1] - y; | ||
})); | ||
}))); |
@@ -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){let f=0,t=0,i=0,r=0,o=0,l=0,u=0,s=0,c=null,d=null;const p=e[0],y=e[1],g=n.length;for(;f<g;f++){t=0;const g=n[f].length-1,h=n[f];for(o=(c=h[0])[0]-p,l=c[1]-y;t<g;t++)if(s=(d=h[t+1])[1]-y,l<0&&s<0||l>0&&s>0)l=s,o=(c=d)[0]-p;else{if(u=d[0]-e[0],s>0&&l<=0){if((r=o*s-u*l)>0)i+=1;else if(0===r)return 0}else if(l>0&&s<=0){if((r=o*s-u*l)<0)i+=1;else if(0===r)return 0}else if(0===s&&l<0){if(0==(r=o*s-u*l))return 0}else if(0===l&&s<0){if(0==(r=o*s-u*l))return 0}else if(0===l&&0===s){if(u<=0&&o>=0)return 0;if(o<=0&&u>=0)return 0}c=d,l=s,o=u}}return i%2!=0}}); | ||
!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){let t=0,i=0,f=0,r=0,o=0,s=0,l=0,u=0,d=null,c=null;const a=e[0],p=e[1],h=n.length;for(;t<h;t++){i=0;const h=n[t].length-1,m=n[t];if((d=m[0])[0]!==m[h][0]&&d[1]!==m[h][1])throw new Error("First and last coordinates in a ring must be the same");for(o=d[0]-a,s=d[1]-p;i<h;i++)if(u=(c=m[i+1])[1]-p,s<0&&u<0||s>0&&u>0)s=u,o=(d=c)[0]-a;else{if(l=c[0]-e[0],u>0&&s<=0){if((r=o*u-l*s)>0)f+=1;else if(0===r)return 0}else if(s>0&&u<=0){if((r=o*u-l*s)<0)f+=1;else if(0===r)return 0}else if(0===u&&s<0){if(0==(r=o*u-l*s))return 0}else if(0===s&&u<0){if(0==(r=o*u-l*s))return 0}else if(0===s&&0===u){if(l<=0&&o>=0)return 0;if(o<=0&&l>=0)return 0}d=c,s=u,o=l}}return f%2!=0}}); |
{ | ||
"name": "point-in-polygon-hao", | ||
"version": "0.0.7", | ||
"version": "1.0.0", | ||
"description": "A point in polygon based on the paper Optimal Reliable Point-in-Polygon Test and Differential Coding Boolean Operations on Polygons", | ||
@@ -12,8 +12,9 @@ "main": "dist/pointInPolygon.js", | ||
"build": "rollup -c", | ||
"test": "ava -v" | ||
"test": "ava" | ||
}, | ||
"dependencies": {}, | ||
"dependencies": { | ||
}, | ||
"devDependencies": { | ||
"@turf/boolean-point-in-polygon": "^6.0.1", | ||
"ava": "^1.3.1", | ||
"ava": "^3.15.0", | ||
"benchmark": "^2.1.4", | ||
@@ -23,3 +24,2 @@ "eslint": "^5.15.3", | ||
"esm": "^3.2.20", | ||
"jsts": "^2.0.3", | ||
"load-json-file": "^5.2.0", | ||
@@ -33,7 +33,8 @@ "point-in-polygon": "^1.0.1", | ||
"files": [ | ||
"test/*spec.js" | ||
"test/*.spec.js" | ||
], | ||
"require": [ | ||
"esm" | ||
] | ||
], | ||
"verbose": true | ||
}, | ||
@@ -40,0 +41,0 @@ "keywords": [ |
@@ -1,7 +0,7 @@ | ||
Based on the paper [Optimal Reliable Point-in-Polygon Test and | ||
Differential Coding Boolean Operations on Polygons](https://www.researchgate.net/publication/328261365_Optimal_Reliable_Point-in-Polygon_Test_and_Differential_Coding_Boolean_Operations_on_Polygons) | ||
A small library for detecting in a point lies inside a polygon | ||
**Features** | ||
- Works on polygons with holes | ||
- Works with degenerate polyons | ||
- Reports 0 if on the edge | ||
- Works with degenerate/self-intersecting polyons | ||
- Returns `0` if on the edge | ||
- Not effected by floating point errors | ||
@@ -14,14 +14,36 @@ | ||
```` | ||
const inside = require('point-in-polygon-hao') | ||
const polygon = [ [ [ 1, 1 ], [ 1, 2 ], [ 2, 2 ], [ 2, 1 ], [ 1, 1 ] ] ]; | ||
import inside from 'point-in-polygon-hao' | ||
const polygon = [ | ||
[ | ||
[1, 1], | ||
[1, 2], | ||
[2, 2], | ||
[2, 1], | ||
[1, 1] | ||
] | ||
]; | ||
inside([ 1.5, 1.5 ], polygon) | ||
// returns true | ||
// => true | ||
inside([ 4.9, 1.2 ], polygon) | ||
// returns false | ||
// => false | ||
inside([1, 2], polygon) | ||
// returns 0 to indicate on edge | ||
// => 0 to indicate on edge | ||
```` | ||
**Note:** The input polygon format aligns with the GeoJson specification for polygons. This means that the first and last coordinate in a polygon must be repeated, if not this library will throw an error. | ||
```` | ||
const polygonWithHole = [ | ||
[ | ||
[0, 0], [1, 0], [1, 1], [0, 1], [0, 0] | ||
], | ||
[ | ||
[0.1, 0.1], [0.1, 0.9], [0.9, 0.9], [0.9, 0.1], [0.1, 0.1] | ||
] | ||
] | ||
```` | ||
The library does not support multi-polygons. | ||
@@ -33,5 +55,11 @@ ### Comparisons | ||
```` | ||
// 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) | ||
```` | ||
```` | ||
// For a point in bounding box check | ||
point-in-poly-hao x 29,365,704 ops/sec ±1.30% (90 runs sampled) | ||
turf-point-in-polygon x 7,142,567 ops/sec ±0.61% (93 runs sampled) | ||
point-in-polygon x 42,339,450 ops/sec ±0.78% (95 runs sampled) | ||
@@ -41,12 +69,8 @@ robust-point-in-polygon x 20,675,569 ops/sec ±0.65% (95 runs sampled) | ||
```` | ||
// For a point in a much larger geometry (700+ vertices) | ||
point-in-poly-hao x 474,180 ops/sec ±0.55% (93 runs sampled) | ||
turf-point-in-polygon x 214,584 ops/sec ±0.74% (95 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) | ||
```` | ||
### Algorithm | ||
This library is based on the paper [Optimal Reliable Point-in-Polygon Test and | ||
Differential Coding Boolean Operations on Polygons](https://www.researchgate.net/publication/328261365_Optimal_Reliable_Point-in-Polygon_Test_and_Differential_Coding_Boolean_Operations_on_Polygons) | ||
### Other notes | ||
* Works irrespective of winding order of polygon | ||
* Does not appear to be effected by flaoting point errors compared to `point-in-polygon` | ||
* Does not appear to be effected by floating point errors compared to `point-in-polygon` or `robust-point-in-polygon` |
@@ -23,2 +23,7 @@ export default function pointInPolygon(p, polygon) { | ||
currentP = contour[0] | ||
if (currentP[0] !== contour[contourLen][0] && | ||
currentP[1] !== contour[contourLen][1]) { | ||
throw new Error('First and last coordinates in a ring must be the same') | ||
} | ||
u1 = currentP[0] - x | ||
@@ -25,0 +30,0 @@ v1 = currentP[1] - y |
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
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
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
10715
11
10
0
74
154