Well, it was a bit harder than expected, because there is no documentation. There is a JavaDoc, but there are nearly no useful comments, so it is just a list of classes and method, useless.
I had to rely on the code itself to figure out how to use the library, and it is probably not the best way...
Anyway, here is a simple test code:
- import org.poly2tri.triangulation.point.*;
- import org.poly2tri.triangulation.util.*;
- import org.poly2tri.transform.coordinate.*;
- import org.poly2tri.triangulation.sets.*;
- import org.poly2tri.triangulation.delaunay.sweep.*;
- import org.poly2tri.triangulation.delaunay.*;
- import org.poly2tri.geometry.primitives.*;
- import org.poly2tri.triangulation.*;
- import org.poly2tri.geometry.polygon.*;
- import org.poly2tri.*;
-
- boolean bShowTriangulation;
- //~ ArrayList<TriangulationPoint> points = new ArrayList<TriangulationPoint>();
- ArrayList<PolygonPoint> points = new ArrayList<PolygonPoint>();
- List<DelaunayTriangle> triangles;
-
- void setup()
- {
- size(500, 500);
- smooth();
- }
-
- void draw()
- {
- background(128);
- fill(255);
- stroke(#0044FF);
- strokeWeight(4);
- if (points.size() < 3)
- {
- for (TriangulationPoint point : points)
- {
- float x = point.getXf();
- float y = point.getYf();
- ellipse(x, y, 3, 3);
- }
- return;
- }
- beginShape();
- for (PolygonPoint point : points)
- {
- //~ println("ShPt: " + point);
- float x = point.getXf();
- float y = point.getYf();
- vertex(x, y);
- }
- endShape(CLOSE);
-
- if (bShowTriangulation && triangles != null)
- {
- stroke(#FF7777);
- strokeWeight(1);
- for (DelaunayTriangle triangle : triangles)
- {
- //~ println("Tr: " + triangle);
- float fx = 0, fy = 0;
- float px = 0, py = 0;
- boolean bFirst = true;
- for (TriangulationPoint point : triangle.points)
- {
- //~ println("TrPt: " + point);
- float x = point.getXf();
- float y = point.getYf();
- if (bFirst)
- {
- fx = x; fy = y;
- bFirst = false;
- }
- else
- {
- line(px, py, x, y);
- }
- px = x; py = y;
- }
- line(px, py, fx, fy);
- }
- }
- }
-
- void mousePressed()
- {
- PolygonPoint point = new PolygonPoint((float) mouseX, (float) mouseY);
- points.add(point);
- }
-
- void keyReleased()
- {
- switch (key)
- {
- case 't':
- DoTriangulation();
- break;
- case 'c':
- points.clear();
- triangles = null;
- bShowTriangulation = false;
- break;
- }
- }
-
- void DoTriangulation()
- {
- if (points.size() < 3)
- return;
-
- // Only DTSweep algorithm available
- TriangulationContext<?> context = Poly2Tri.createContext(TriangulationAlgorithm.DTSweep);
- //~ context.addPoints(points);
- Polygon polygon = new Polygon(points);
- context.prepareTriangulation(polygon);
- Poly2Tri.triangulate(context);
-
- triangles = context.getTriangles();
- //~ println("DT: " + triangles);
- //~ context.clear(); // Remove the points
-
- bShowTriangulation = true;
- }
Click to drawn a polygon, once it is closed you can type t to triangulate, c to clear all.
The code needs some more work, to eliminate triangles outside of the polygon.
I had a bad surprise: the library is dependent on another library, a log framework (
SLF4J). I don't think such kind of library should rely on a logger, at least for the released version, but well, you have to download the binary of SLF4J and extract slf4j-api-1.6.1.jar and slf4j-simple-1.6.1.jar next to the poly2tri.jar in the library folder.