/polygon.clip.js

clip arbitrary polygons

Primary LanguageJavaScript

polygon.clip

Implementation of the Greiner-Kai "efficient clipping of arbitrary polygons" paper

install

npm install poygon.clip npm install vec2

use

signature

Polygon#clip(clippingPolygon, mode)

Where mode is 'difference' or 'union'

example

Lets clip two squares

var Polygon = require('polygon.clip'),
    Vec2 = require('vec2');

var subject = Polygon([
  Vec2(0, 0),
  Vec2(100, 0),
  Vec2(100, 100),
  Vec2(0, 100),
  Vec2(0, 0)
]);

var clip = Polygon([
  Vec2(90, 90),
  Vec2(110, 90),
  Vec2(110, 110),
  Vec2(90, 110),
  Vec2(90, 90)
]);


// union is an array of Polygons
var union = subject.clip(clip, 'union');

In this case, there will only be one polygon in the union array, and it will define the upper corner of the subject polygon

[[100, 90], [100,100], [90, 100], [90,90]]

license

MIT