Boolean operations on polygons
npm install flatten-boolean-op


javascript
// require flatten-boolean-op package
let BooleanOp = require('flatten-boolean-op');
let {unify, intersect, subtract} = BooleanOp;
// require flatten-js library that provides polygon model and other geometrical primitives
let Flatten = require('flatten-js');
let {Polygon, point} = Flatten;
// create first polygon
let poly1 = new Polygon();
poly1.addFace([point(0,0), point(150, 0), point(150,30), point(0, 30)]);
// create second polygon
let poly2 = new Polygon();
poly2.addFace([point(100, 20), point(200, 20), point(200, 40), point(100, 40)]);
// apply boolean operation "unify"
let poly = unify(poly1, poly2);
console.log(poly.faces.size); // expected 1
console.log(poly.edges.size); // expected 8
`
Consume as a service
Instead of installing the package in your project you may choose to consume it as a service from
1. Define your account in Algorithmia platform and get your API key
2. Install Algorithmia client
npm install --save algorithmia
3. Instantiate an Algorithmia client using your API key:
`javascript
var algorithmia = require("algorithmia");
var client = algorithmia(process.env.ALGORITHMIA_API_KEY);
`
4. Submit new call to boolean operation algorithm using Algorithmia API
Algorithm accepts input as an array of 3 elements: two polygon operands and boolean operation identifier.
Boolean operation identifier is one of Flatten.BOOLEAN_UNION, Flatten.BOOLEAN_SUBTRACT, Flatten.BOOLEAN_INTERSECT constants.
Polygon operand is JSON object, which is array of faces, where each face represented as an array of edges.
Polygon JSON object may be obtained using build-in .toJSON() method of polygon.
Algorithmia client calls JSON.serialize(), submits query to the Algorithmia server and returns promise.
When promise resolved, it returns the result as a polygon JSON object - array of faces
Each face may be added to polygon using .addFace() method
`javascript
// require Algorithmia client and instantiate it using your API key
let algorithmia = require("algorithmia");
let client = algorithmia(process.env.ALGORITHMIA_API_KEY);
// require flatten-js library that provides polygon model and other geometrical primitives
let Flatten = require('flatten-js');
let {Polygon, point} = Flatten;
// create first polygon
let poly1 = new Polygon();
poly1.addFace([point(0,0), point(150, 0), point(150,30), point(0, 30)]);
// create second polygon
let poly2 = new Polygon();
poly2.addFace([point(100, 20), point(200, 20), point(200, 40), point(100, 40)]);
// prepare input array for calling algorithm on cloud
let input = [poly1.toJSON(), poly2.toJSON(), Flatten.BOOLEAN_UNION];
// submit query
client("ALGORITHMIA_API_KEY")
.algo("alexbol99/PolygonBooleanOp/0.1.2")
.pipe(input)
.then(function(output) {
// Parse response from Algorithmia and create new polygon
let poly = new Polygon();
for (let jsonFace of output) {
poly.addFace(jsonFace);
}
console.log(poly.faces.size); // expected 1
console.log(poly.edges.size); // expected 8
});
``