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
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
dist v1.4.1 Sep 4, 2018
lib initial commit Aug 4, 2014
src es6 + rollup update Sep 4, 2018
test es6 + rollup update Sep 4, 2018
.gitignore es6 + rollup update Sep 4, 2018
LICENSE Update LICENSE Oct 12, 2015
README.md Update README.md Aug 9, 2016
bower.json bower Oct 17, 2015
index.d.ts Added type script definition. Aug 13, 2017
package-lock.json es6 + rollup update Sep 4, 2018
package.json v1.4.1 Sep 4, 2018
rollup.config.js es6 + rollup update Sep 4, 2018

README.md

License Greiner-Hormann polygon clipping

  • Does AND, OR, XOR (intersection, union, difference, if you're human)
  • Plays nicely with Leaflet, comes with an adaptor for it
  • Handles non-convex polygons and multiple clipping areas
  • ~3kb compressed, no dependencies

Demo and documentation

Note: If you are looking for something more powerful, take a look at the Martinez polygon clipping implementation.

Install

$ npm install greiner-hormann

Browserify

var greinerHormann = require('greiner-hormann');

Browser

<script src="path/to/greiner-hormann(.leaflet).min.js"></script>

Use

...
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.