Skip to content

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

License

Notifications You must be signed in to change notification settings

platener/GreinerHormann

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

33 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

License Greiner-Hormann polygon clipping (fork)

  • 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

Changes in this fork

  • This fork implements the degeneracy problem fix as proposed by Greiner/Hormann. It moves problematic points outwards by 0.000000001 units.
  • It also removes the polygon winding dependency of the algorithm by detecting the winding.

Demo and documentation

Install

$ npm install platener/GreinerHormann

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.

About

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

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • JavaScript 97.7%
  • Makefile 1.3%
  • Other 1.0%