|
GeneralPolygonClipperAlgorithm
General Polygon Clipper algorithm
A haXe port of the General Polygon Clipper (GPC) library developed by Alan MurtaHomepage: http://www.cs.manchester.ac.uk/~toby/alan/software/ GPC license http://www.cs.manchester.ac.uk/~toby/alan/software/#Licensing GPC is free for non-commercial use ONLY. If you wish to use GPC in support of a commercial product, you must obtain an official GPC Commercial Use Licence from The University of Manchester. AboutGPC is part of the motor library and can be found in the package de.polygonal.motor.geom.gpc. It consists of merely two classes: GPC.hx (API), the clipping algorithm and GPCPolygon.hx (API), which is a data structure that is used by the GPC class to describe polygons (both input & clipped polygons). ExamplesGreat Britain and Arrows example from GPC homepage ("GPC in Action") Great Britain and Arrows example (animated) The source files of the examples can be found here. To compile them run $ haxe compile.hxml in the gpc folder. UsageHere is a simple example that computes the union of two bounding boxes: import de.polygonal.gl.VectorRenderer;
import de.polygonal.gl.Window;
import de.polygonal.motor.geom.gpc.GPC;
import de.polygonal.motor.geom.gpc.GPCPolygon;
import de.polygonal.motor.geom.primitive.AABB2;
class SimpleExample
{
public static function main() {
Window.initBackgroundColor = 0xffffff;
de.polygonal.core.Root.init(onInit, true);
}
static function onInit() {
//the input shapes
var box1 = new AABB2(100, 100, 200, 200);
var box2 = new AABB2(150, 150, 250, 250);
//define first input polygon
var input1 = new GPCPolygon();
input1.addContour(box1.getVertexListScalar(new Array<Float>()), 8);
//define second input polygon
var input2 = new GPCPolygon();
input2.addContour(box2.getVertexListScalar(new Array<Float>()), 8);
//define output polygon
var output = new GPCPolygon();
//compute union of input1 and input
GPC.clip(input1, input2, output, ClipOperation.Union);
var vr = new VectorRenderer();
//draw input
vr.setLineStyle();
vr.aabb(box1);
vr.aabb(box2);
//draw output
vr.setLineStyle(0xff0000, .5, 4);
for (contour in output) vr.polyLineScalar(contour.getArray(), true, contour.size());
//draw everything
vr.flush(Window.surface.graphics);
//release memory used by GPC class (if never used again)
GPC.free();
}
}
|