# polygon-clipping

> Apply boolean Polygon clipping operations (intersection, union, difference, xor) to your Polygons & MultiPolygons.

Latest version **0.15.7** (published 2023-12-18) · MIT license · 0 weekly downloads

## Install

```sh
npm install polygon-clipping
pnpm add polygon-clipping
yarn add polygon-clipping
bun add polygon-clipping
```

## Health

**Score 30/100 (F)** — status: abandoned.

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

Warnings: low downloads; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.15.7 |
| Published | 2023-12-18 |
| First published | 2018-02-27 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 2 |
| Unpacked size | 342 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 641 |
| Author | Mike Fogel |
| Maintainers | mfogel |
| Keywords | polygon, clipping, boolean, intersection, union, difference, xor, geometry, martinez |

## Links

- npm: https://www.npmjs.com/package/polygon-clipping
- Repository: https://github.com/mfogel/polygon-clipping
- Homepage: https://github.com/mfogel/polygon-clipping#readme
- Issues: https://github.com/mfogel/polygon-clipping/issues
- npm.io page: https://npm.io/package/polygon-clipping

## Dependencies (2)

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

## Recent versions

- 0.15.7 (latest) — 2023-12-18
- 0.15.6 — 2023-12-18
- 0.15.5 — 2023-12-17
- 0.15.4 — 2023-12-17
- 0.15.3 — 2021-04-20
- 0.15.2 — 2020-12-15
- 0.15.1 — 2020-07-08
- 0.15.0 — 2020-07-03
- 0.14.3 — 2019-10-26
- 0.14.2 — 2019-07-27
- 0.14.1 — 2019-05-14
- 0.14.0 — 2019-03-30
- 0.13.0 — 2019-02-26
- 0.12.3 — 2019-02-14
- 0.12.2 — 2019-01-29
- … 14 more at https://npm.io/package/polygon-clipping/versions

## README

# polygon-clipping

Apply boolean Polygon clipping operations (`intersection`, `union`, `difference`, `xor`) to your Polygons & MultiPolygons.

[![CI](https://github.com/mfogel/polygon-clipping/actions/workflows/ci.yml/badge.svg?branch=main)](https://github.com/mfogel/polygon-clipping/actions) [![codecov](https://codecov.io/gh/mfogel/polygon-clipping/branch/main/graph/badge.svg?token=is93inDQiJ)](https://codecov.io/gh/mfogel/polygon-clipping) [![License: MIT](https://img.shields.io/badge/License-MIT-green.svg)](https://opensource.org/licenses/MIT) [![npm](https://img.shields.io/npm/v/polygon-clipping.svg)](https://www.npmjs.com/package/polygon-clipping)

## Quickstart

<!-- prettier-ignore-start -->
```javascript
const polygonClipping = require('polygon-clipping')

const poly1 = [[[0,0],[2,0],[0,2],[0,0]]]
const poly2 = [[[-1,0],[1,0],[0,1],[-1,0]]]

polygonClipping.union       (poly1, poly2 /* , poly3, ... */)
polygonClipping.intersection(poly1, poly2 /* , poly3, ... */)
polygonClipping.xor         (poly1, poly2 /* , poly3, ... */)
polygonClipping.difference  (poly1, poly2 /* , poly3, ... */)
```
<!-- prettier-ignore-end -->

## API

```javascript
/* All functions take one or more [multi]polygon(s) as input */

polygonClipping.union       (<geom>, ...<geoms>)
polygonClipping.intersection(<geom>, ...<geoms>)
polygonClipping.xor         (<geom>, ...<geoms>)

/* The clipGeoms will be subtracted from the subjectGeom */
polygonClipping.difference(<subjectGeom>, ...<clipGeoms>)
```

### Input

Each positional argument (`<geom>`) may be either a Polygon or a MultiPolygon. The [GeoJSON spec](https://tools.ietf.org/html/rfc7946#section-3.1) is followed, with the following notes/modifications:

- MultiPolygons may contain touching or overlapping Polygons.
- rings are not required to be self-closing.
- rings may contain repeated points, which are ignored.
- rings may be self-touching and/or self-crossing. Self-crossing rings will be interpreted using the [non-zero rule](https://en.wikipedia.org/wiki/Nonzero-rule).
- winding order of rings does not matter.
- inner rings may extend outside their outer ring. The portion of inner rings outside their outer ring is dropped.
- inner rings may touch or overlap each other.

### Output

For non-empty results, output will always be a MultiPolygon containing one or more non-overlapping, non-edge-sharing Polygons. The [GeoJSON spec](https://tools.ietf.org/html/rfc7946#section-3.1) is followed, with the following notes/modifications:

- outer rings will be wound counter-clockwise, and inner rings clockwise.
- inner rings will not extend outside their outer ring.
- rings will not overlap, nor share an edge with each other.
- rings will be self-closing.
- rings will not contain repeated points.
- rings will not contain superfluous points (intermediate points along a straight line).
- rings will not be self-touching nor self-crossing.
- rings _may_ touch each other, but _may not_ cross each other.

In the event that the result of the operation is the empty set, output will be a MultiPolygon with no Polygons: `[]`.

## Correctness

Run: `npm test`

The tests are broken up into unit tests and end-to-end tests. The end-to-end tests are organized as GeoJSON files, to make them easy to visualize thanks to [GitHub's helpful rendering of GeoJSON files](https://help.github.com/articles/mapping-geojson-files-on-github/). Browse those tests [here](test/end-to-end).

## Performance

The Martinez-Rueda-Feito polygon clipping algorithm is used to compute the result in `O((n+k)*log(n))` time, where `n` is the total number of edges in all polygons involved and `k` is the number of intersections between edges.

## Settings

Global settings are set via environment variables.

- **POLYGON_CLIPPING_MAX_QUEUE_SIZE** and **POLYGON_CLIPPING_MAX_SWEEPLINE_SEGMENTS**: Aims to prevent infinite loops - usually caused by floating-point math round-off errors. Defaults are 1,000,000.

## Changelog

This project adheres to [Semantic Versioning](https://semver.org/).

<!-- prettier-ignore-end -->

The full changelog is available at [CHANGELOG.md](https://github.com/mfogel/polygon-clipping/blob/master/CHANGELOG.md).

## Authors

- [Mike Fogel](https://github.com/mfogel)
- [Alexander Milevski](https://github.com/w8r)
- [Vladimir Ovsyannikov](https://github.com/sh1ng)

## Sponsors

- [Alantgeo](https://www.alantgeo.com.au/)
- [EasyTerritory](https://www.easyterritory.com/)

Please contact [Mike Fogel](https://github.com/mfogel) if you or your company is interested in sponsoring work on specific bug fixes or feature requests.

## Based on

- [A new algorithm for computing Boolean operations on polygons](paper.pdf) by Francisco Martinez, Antonio Jesus Rueda, Francisco Ramon Feito (2009)

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