# point-in-polygon-hao

> A point in polygon based on the paper Optimal Reliable Point-in-Polygon Test and Differential Coding Boolean Operations on Polygons

Latest version **1.2.4** (published 2024-12-22) · MIT license · 0 weekly downloads

## Install

```sh
npm install point-in-polygon-hao
pnpm add point-in-polygon-hao
yarn add point-in-polygon-hao
bun add point-in-polygon-hao
```

## Health

**Score 40/100 (D)** — status: maintenance-mode.

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads.

Negative: stale; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.2.4 |
| Published | 2024-12-22 |
| First published | 2019-03-22 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 1 |
| Unpacked size | 28.2 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 149 |
| Author | Rowan Winsemius |
| Maintainers | rowanwins |
| Keywords | point-in-polygon, pip, point, polygon, inside |

## Links

- npm: https://www.npmjs.com/package/point-in-polygon-hao
- Repository: https://github.com/rowanwins/point-in-polygon-hao
- Issues: https://github.com/rowanwins/point-in-polygon-hao/issues
- npm.io page: https://npm.io/package/point-in-polygon-hao

## Dependencies (1)

- [robust-predicates](https://npm.io/package/robust-predicates.md) ^3.0.2

## Alternatives

- [byte-size](https://npm.io/package/byte-size.md) — 2.1M weekly downloads
- [speed-limiter](https://npm.io/package/speed-limiter.md) — 16.0K weekly downloads
- [@powersync/node](https://npm.io/package/@powersync/node.md) — 10.9K weekly downloads
- [@ledgerhq/coin-cardano](https://npm.io/package/@ledgerhq/coin-cardano.md) — 1.0K weekly downloads
- [@jayesol/jayeson.lib.streamfinder](https://npm.io/package/@jayesol/jayeson.lib.streamfinder.md) — 1.0K weekly downloads

## Recent versions

- 1.2.4 (latest) — 2024-12-22
- 1.2.3 — 2024-12-03
- 1.2.2 — 2024-12-02
- 1.2.1 — 2024-12-01
- 1.2.0 — 2024-11-28
- 1.1.0 — 2021-08-05
- 1.0.0 — 2021-06-02
- 0.0.7 — 2020-05-01
- 0.0.6 — 2019-07-28
- 0.0.5 — 2019-07-28
- 0.0.4 — 2019-07-22
- 0.0.3 — 2019-04-05
- 0.0.2 — 2019-03-22
- 0.0.1 — 2019-03-22

## README

A small library for detecting in a point lies inside a polygon

**Features**
- Works on polygons with holes
- Works with degenerate/self-intersecting polyons 
- Returns `0` if on the edge
- Not effected by floating point errors


### Usage
Install via `npm install point-in-polygon-hao`

````
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)
// => true

inside([ 4.9, 1.2 ], polygon)
// => false

inside([1, 2], polygon)
// => 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.

### Comparisons
Some rough comparisons to similar libraries. 
While `point-in-polygon` is slightly faster in most cases it does not support polygons with holes or degenerate polygons.

````
// For a point in a much larger geometry (700+ vertices)
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)
````

````
// For a point in bounding box check
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)
````

### 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 floating point errors compared to `point-in-polygon` or `robust-point-in-polygon`~~
  * Added robust-predicates to deal with some floating point errors

---
_Source: https://npm.io/package/point-in-polygon-hao · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
