These are Ruby bindings to Clipper, Angus Johnson's Polygon clipping library. Because Clipper is not readily packaged, and is so beautifully self-contained, I've included the two required files in the package.
This release contains version 6.2.1 of Clipper.
gem install clipper
This shold be enough to get you started. Full documentation is below.
require 'clipper' a = [[0, 0], [0, 100], [100, 100], [100, 0]] b = [[-5, 50], [200, 50], [100, 5]] c = Clipper::Clipper.new c.add_subject_polygon(a) c.add_clip_polygon(b) c.union :non_zero, :non_zero => [[[100.0, 0.0], [0.0, 0.0], [0.0, 47.85714326530613], [-4.999999, 50.0], [0.0, 50.0], [0.0, 100.0], [100.0, 100.0], [100.0, 50.0], [200.0, 50.0], [100.0, 5.0]]]
Clipper is a two-dimensional polygon clipping library.
rbclipper, the Ruby
bindings can be accessed by:
Operations that accept or return polygons are specified as an array of
coordinates, for example, to specify a triangle:
triangle = [[0,0], [0,100], [50, -100]]
Clipper supports both holes and complex polygons. Coordinates for output polygons are clockwise for shells, and and counter-clockwise for holes. See force_orientation.
Note that since 2.8, Clipper defines orientation with respect to a downward-increasing Y axis, similar to how many 2D GUI/drawing APIs position coordinate (0,0) at the top-left corner. The bindings have followed Clipper proper in this regard.
Multiple polygons are represented as simply an array of polygons.
A point is considered inside the polygon if the number of edge-crossings to get there from outside the shape is an even number.
A point is considered inside the polygon if the number of edge-crossings to get there is greater than zero.
Creates a new clipper object.
Adds a subject or clip polygon to the engine. Boolean operations are calculated as
CLIP. Multiple polygons can Pay attention to the orientation of the coordinates given; counter-clockwise for shells and clockwise for holes.
Multiple subject and clip polygons can be added to the engine for operations.
Add a "Poly-Polygon" to the engine. Which is basically a set of polygons.
Boolean operations consider every poly-polygon added in this manner to be the same object.
Defaults to true. Ensures that the simple result of boolean operations have the orientation as described in section Polygons. Only useful with simple polygons.
Performs a boolean operation on the polygons that have been added to the clipper object. The result is a list of polygons.