|
1 | | -Based on the paper [Optimal Reliable Point-in-Polygon Test and |
2 | | -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) |
| 1 | +A small library for detecting in a point lies inside a polygon |
3 | 2 |
|
| 3 | +**Features** |
4 | 4 | - Works on polygons with holes |
5 | | -- Works with degenerate polyons |
6 | | -- Reports 0 if on the edge |
| 5 | +- Works with degenerate/self-intersecting polyons |
| 6 | +- Returns `0` if on the edge |
7 | 7 | - Not effected by floating point errors |
8 | 8 |
|
9 | 9 |
|
10 | 10 | ### Usage |
11 | 11 | Install via `npm install point-in-polygon-hao` |
12 | 12 |
|
13 | 13 | ```` |
14 | | -const inside = require('point-in-polygon-hao') |
15 | | -const polygon = [ [ [ 1, 1 ], [ 1, 2 ], [ 2, 2 ], [ 2, 1 ], [ 1, 1 ] ] ]; |
| 14 | +import inside from 'point-in-polygon-hao' |
| 15 | +
|
| 16 | +const polygon = [ |
| 17 | + [ |
| 18 | + [1, 1], |
| 19 | + [1, 2], |
| 20 | + [2, 2], |
| 21 | + [2, 1], |
| 22 | + [1, 1] |
| 23 | + ] |
| 24 | +]; |
| 25 | +
|
16 | 26 | inside([ 1.5, 1.5 ], polygon) |
17 | | -// returns true |
| 27 | +// => true |
18 | 28 |
|
19 | 29 | inside([ 4.9, 1.2 ], polygon) |
20 | | -// returns false |
| 30 | +// => false |
21 | 31 |
|
22 | 32 | inside([1, 2], polygon) |
23 | | -// returns 0 to indicate on edge |
| 33 | +// => 0 to indicate on edge |
24 | 34 | ```` |
25 | 35 |
|
| 36 | +**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. |
| 37 | +```` |
| 38 | +const polygonWithHole = [ |
| 39 | + [ |
| 40 | + [0, 0], [1, 0], [1, 1], [0, 1], [0, 0] |
| 41 | + ], |
| 42 | + [ |
| 43 | + [0.1, 0.1], [0.1, 0.9], [0.9, 0.9], [0.9, 0.1], [0.1, 0.1] |
| 44 | + ] |
| 45 | +] |
| 46 | +```` |
| 47 | +The library does not support multi-polygons. |
26 | 48 |
|
27 | 49 | ### Comparisons |
28 | 50 | Some rough comparisons to similar libraries. |
29 | 51 | While `point-in-polygon` is slightly faster in most cases it does not support polygons with holes or degenerate polygons. |
30 | 52 |
|
| 53 | +```` |
| 54 | +// For a point in a much larger geometry (700+ vertices) |
| 55 | +point-in-poly-hao x 474,180 ops/sec ±0.55% (93 runs sampled) |
| 56 | +point-in-polygon x 489,649 ops/sec ±0.75% (91 runs sampled) |
| 57 | +robust-point-in-polygon x 376,268 ops/sec ±0.79% (89 runs sampled) |
| 58 | +```` |
| 59 | + |
31 | 60 | ```` |
32 | 61 | // For a point in bounding box check |
33 | 62 | point-in-poly-hao x 29,365,704 ops/sec ±1.30% (90 runs sampled) |
34 | | -turf-point-in-polygon x 7,142,567 ops/sec ±0.61% (93 runs sampled) |
35 | 63 | point-in-polygon x 42,339,450 ops/sec ±0.78% (95 runs sampled) |
36 | 64 | robust-point-in-polygon x 20,675,569 ops/sec ±0.65% (95 runs sampled) |
37 | 65 | ```` |
38 | 66 |
|
39 | | -```` |
40 | | -// For a point in a much larger geometry (700+ vertices) |
41 | | -point-in-poly-hao x 474,180 ops/sec ±0.55% (93 runs sampled) |
42 | | -turf-point-in-polygon x 214,584 ops/sec ±0.74% (95 runs sampled) |
43 | | -point-in-polygon x 489,649 ops/sec ±0.75% (91 runs sampled) |
44 | | -robust-point-in-polygon x 376,268 ops/sec ±0.79% (89 runs sampled) |
45 | | -```` |
| 67 | +### Algorithm |
| 68 | +This library is based on the paper [Optimal Reliable Point-in-Polygon Test and |
| 69 | +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) |
46 | 70 |
|
47 | 71 | ### Other notes |
48 | 72 | * Works irrespective of winding order of polygon |
49 | | -* Does not appear to be effected by flaoting point errors compared to `point-in-polygon` |
| 73 | +* Does not appear to be effected by floating point errors compared to `point-in-polygon` or `robust-point-in-polygon` |
0 commit comments