To port the d3-delaunay library to Java using the delaunator-java library by waveware4ai
V1.1: added new float APIs
Tested with Processing 4 on macOS (Intel x64) and Windows10/11
You can download the package from the here, then import it into processing as a library. (you might need the newest Processing 4 to use it)
Or you can also build it from source. I am using gradle to package it. To do so:
clone the repo to your machine: git clone https://github.com/Real-John-Cheung/d3-delaunay-for-processing.git
after that, you can make some change according to your needs and run
gradle clean shadowJar
to build the library.
Packed .jar
file will be in ./lib/build/libs
import d3delaunayforprocessing.*;
double[] points = new double[40];
void setup() {
size(600,600);
for (int i = 0; i < points.length; i ++){
points[i] = random(10,590);
}
Delaunay d = new Delaunay(points);
background(255);
noFill();
stroke(0);
strokeWeight(3);
for (int i = 0; i < points.length; i +=2){
point((float)points[i], (float)points[i+1]);
}
strokeWeight(0.5);
Voronoi v = d.voronoi(new double[] {0,0,width,height});
for (int i = 0; i < points.length/2; i ++){
double[][] cell = v.cellPolygon(i);
if (cell == null) continue;
beginShape();
for (int j = 0; j < cell.length; j ++){
vertex((float)cell[j][0],(float)cell[j][1]);
}
endShape();
}
stroke(255,0,0);
double[][][] triangles = d.trianglePolygons();
for(int i = 0; i < triangles.length; i ++) {
double[][] tri = triangles[i];
beginShape();
for (int j = 0; j < tri.length; j ++) {
double[] p = tri[j];
vertex((float)p[0], (float)p[1]);
}
endShape();
}
}
On github, https://github.com/Real-John-Cheung/d3-delaunay-for-processing
By John C