Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/w8r/greinerhormann
Greiner-Hormann polygon clipping algorithm. Does AND, OR, XOR. Plays nicely with Leaflet. Handles non-convex polygons and multiple clipping areas. ~3kb footprint, no dependencies
https://github.com/w8r/greinerhormann
algorithm computational-geometry leaflet polygon-clipping polygon-intersection polygon-union
Last synced: 3 days ago
JSON representation
Greiner-Hormann polygon clipping algorithm. Does AND, OR, XOR. Plays nicely with Leaflet. Handles non-convex polygons and multiple clipping areas. ~3kb footprint, no dependencies
- Host: GitHub
- URL: https://github.com/w8r/greinerhormann
- Owner: w8r
- License: mit
- Created: 2014-08-01T12:59:28.000Z (over 10 years ago)
- Default Branch: master
- Last Pushed: 2023-04-15T21:28:20.000Z (over 1 year ago)
- Last Synced: 2024-12-16T00:05:18.474Z (10 days ago)
- Topics: algorithm, computational-geometry, leaflet, polygon-clipping, polygon-intersection, polygon-union
- Language: JavaScript
- Homepage: http://w8r.github.io/GreinerHormann/
- Size: 1.74 MB
- Stars: 239
- Watchers: 11
- Forks: 34
- Open Issues: 15
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
- License: LICENSE
Awesome Lists containing this project
README
[![License](http://img.shields.io/badge/license-MIT-brightgreen.svg)](http://opensource.org/licenses/MIT)
Greiner-Hormann polygon clipping
================================* Does AND, OR, difference (intersection, union, difference, if you're human)
* Plays nicely with [Leaflet](http://github.com/leaflet/leaflet/), comes with an adaptor for it
* Handles non-convex polygons and multiple clipping areas
* ~3kb compressed, no dependencies[Demo and documentation](http://w8r.github.io/GreinerHormann/)
**Note:** If you are looking for something more powerful, take a look at the [Martinez polygon clipping](https://github.com/w8r/martinez) implementation.
## Install
```bash
$ npm install greiner-hormann
```Browserify
```js
var greinerHormann = require('greiner-hormann');
```Browser
```html```
## Use
```js
...
var intersection = greinerHormann.intersection(source, clip);
var union = greinerHormann.union(source, clip);
var diff = greinerHormann.diff(source, clip);...
if(intersection){
if(typeof intersection[0][0] === 'number'){ // single linear ring
intersection = [intersection];
}
for(var i = 0, len = intersection.length; i < len; i++){
L.polygon(intersection[i], {...}).addTo(map);
}
}
```## Format
Input and output can be `{x:x, y:y}` objects or `[x,y]` pairs. It will output the points in the same format you put in.