Where poly1, poly2, and the return value are Polygon objects, in the format of:
// polygon format{ regions: [ // list of regions // each region is a list of points [[50,50], [150,150], [190,50]], [[130,50], [290,150], [290,50]] ], inverted: false // is this polygon inverted?}
GeoJSON (experimental)
There are also functions for converting between the native polygon format and
GeoJSON.
Note: These functions are currently experimental, and I'm hoping users can provide feedback.
Please comment in this issue on GitHub -- including
letting me know if it's working as expected. I don't use GeoJSON, but I thought I would take a
crack at conversion functions.
Use the following functions:
var geojson = PolyBool.polygonToGeoJSON(poly);var poly = PolyBool.polygonFromGeoJSON(geojson);
Only "Polygon" and "MultiPolygon" types are supported.
Depending on your needs, it might be more efficient to construct your own sequence of operations
using the lower-level API. Note that PolyBool.union, PolyBool.intersect, etc, are just thin
wrappers for convenience.
There are three types of objects you will encounter in the core API:
Polygons (discussed above, this is a list of regions and an inverted flag)
Segments
Combined Segments
The basic flow chart of the API is:
You start by converting Polygons to Segments using PolyBool.segments(poly).
You convert Segments to Combined Segments using PolyBool.combine(seg1, seg2).
You select the resulting Segments from the Combined Segments using one of the selection operators
PolyBool.selectUnion(combined), PolyBool.selectIntersect(combined), etc. These selection
functions return Segments.
Once you're done, you convert the Segments back to Polygons using PolyBool.polygon(segments).
Each transition is costly, so you want to navigate wisely. The selection transition is the least
costly.
Advanced Example 1
Suppose you wanted to union a list of polygons together. The naive way to do it would be:
// works but not efficientvar result = polygons[0];for (var i = 1; i < polygons.length; i++) result = PolyBool.union(result, polygons[i]);return result;
Instead, it's more efficient to use the core API directly, like this:
// works AND efficientvar segments = PolyBool.segments(polygons[0]);for (var i = 1; i < polygons.length; i++){ var seg2 = PolyBool.segments(polygons[i]); var comb = PolyBool.combine(segments, seg2); segments = PolyBool.selectUnion(comb);}return PolyBool.polygon(segments);
Advanced Example 2
Suppose you want to calculate all operations on two polygons. The naive way to do it would be:
// works but not efficientreturn { union : PolyBool.union (poly1, poly2), intersect : PolyBool.intersect (poly1, poly2), difference : PolyBool.difference (poly1, poly2), differenceRev: PolyBool.differenceRev(poly1, poly2), xor : PolyBool.xor (poly1, poly2)};
Instead, it's more efficient to use the core API directly, like this:
As an added bonus, just going from Polygon to Segments and back performs simplification on the
polygon.
Suppose you have garbage polygon data and just want to clean it up. The naive way to do it would
be:
// union the polygon with nothing in order to clean up the data// works but not efficientvar cleaned = PolyBool.union(polygon, { regions: [], inverted: false });
Instead, skip the combination and selection phase:
// works AND efficientvar cleaned = PolyBool.polygon(PolyBool.segments(polygon));
Epsilon
Due to the beauty of floating point reality, floating point calculations are not exactly perfect.
This is a problem when trying to detect whether lines are on top of each other, or if vertices are
exactly the same.
Normally you would expect this to work:
if (A === B) /* A and B are equal */;else /* A and B are not equal */;
But for inexact floating point math, instead we use:
if (Math.abs(A - B) < epsilon) /* A and B are equal */;else /* A and B are not equal */;
You can set the epsilon value using:
PolyBool.epsilon(newEpsilonValue);
Or, if you just want to get the current value:
var currentEpsilon = PolyBool.epsilon();
The default epsilon value is 0.0000000001.
If your polygons are really really large or really really tiny, then you will probably have to come
up with your own epsilon value -- otherwise, the default should be fine.
If PolyBool detects that your epsilon is too small or too large, it will throw an error:
PolyBool: Zero-length segment detected; your epsilon is probably too small or too large
Build Log
The library also has an option for tracking execution of the internal algorithms. This is useful
for debugging or creating the animation on the demo page.
By default, the logging is disabled. But you can enable or reset it via:
var buildLog = PolyBool.buildLog(true);
The return value is an empty list that will have log entries added to it as more API calls are made.