Skip to content

Commit

Permalink
Fix voronoi centers (#708)
Browse files Browse the repository at this point in the history
* better comments

* draw voronoi centers in the right place

* fix null

* optional show center
  • Loading branch information
i-make-robots authored Oct 2, 2023
1 parent 1f79df9 commit 8aa3090
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public void setNumHulls(int numHulls) {
}

public void tessellate(List<VoronoiCell> points, Rectangle2D bounds, double tolerance) {
if(points.size()!=coordinates.length) setNumHulls(points.size());
if(coordinates==null || points.size()!=coordinates.length) setNumHulls(points.size());

int i=0;
for(VoronoiCell cell : points) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.marginallyclever.convenience.voronoi.VoronoiCell;
import com.marginallyclever.convenience.voronoi.VoronoiTesselator2;
import com.marginallyclever.makelangelo.Translator;
import com.marginallyclever.makelangelo.select.SelectBoolean;
import com.marginallyclever.makelangelo.select.SelectInteger;
import com.marginallyclever.makelangelo.turtle.Turtle;
import org.locationtech.jts.geom.Coordinate;
Expand All @@ -19,6 +20,7 @@
*/
public class Generator_Voronoi extends TurtleGenerator {
private static int numCells = 500;
private static boolean showCenters = false;

public Generator_Voronoi() {
super();
Expand All @@ -29,6 +31,13 @@ public Generator_Voronoi() {
setNumCells(Math.max(1,cells.getValue()));
generate();
});
SelectBoolean showCenterChoice;
add(showCenterChoice = new SelectBoolean("showCenters",Translator.get("Converter_Voronoi.ShowCenters"),false));
showCenterChoice.addPropertyChangeListener(evt->{
showCenters = showCenterChoice.isSelected();
generate();
});

}

@Override
Expand All @@ -48,18 +57,30 @@ public void generate() {
Turtle turtle = new Turtle();

Rectangle2D bounds = myPaper.getMarginRectangle();
List<VoronoiCell> points = seedRandomPoints(bounds);
// generate the voronoi diagram
VoronoiTesselator2 diagram = new VoronoiTesselator2();
diagram.tessellate(points,bounds,0.0001);

drawGraphEdges(turtle,diagram);
if(showCenters) drawCellCenters(turtle,points);
turtle.penUp();
notifyListeners(turtle);
}

// seed random points on the paper.
private List<VoronoiCell> seedRandomPoints(Rectangle2D bounds) {
List<VoronoiCell> points = new ArrayList<>();
for(int i=0;i<numCells;++i) {
points.add(new VoronoiCell(
Math.random()*bounds.getWidth() +bounds.getMinX(),
Math.random()*bounds.getHeight()+bounds.getMinY()));
Math.random()*bounds.getWidth() + bounds.getMinX(),
Math.random()*bounds.getHeight() + bounds.getMinY()));
}
return points;
}

VoronoiTesselator2 diagram = new VoronoiTesselator2();
diagram.tessellate(points,bounds,0.0001);

// draw all the graph edges according to the cells.
// draw all the graph edges according to the cells.
private void drawGraphEdges(Turtle turtle, VoronoiTesselator2 diagram) {
for(int i=0;i<diagram.getNumHulls();++i) {
boolean first = true;
Polygon poly = diagram.getHull(i);
Expand All @@ -70,22 +91,23 @@ public void generate() {
} else turtle.moveTo(p.x, p.y);
}
}
}

private void drawCellCenters(Turtle turtle, List<VoronoiCell> points) {
// draw all the cell centers
turtle.setColor(new ColorRGB(0,0,255));

for( VoronoiCell p : points ) {
// jump to corner
turtle.jumpTo(p.center.x-0.5,p.center.y-0.5);
// box
for(int i=0;i<4;++i) {
turtle.forward(1);
turtle.turn(90);
}
// point in center
turtle.jumpTo(p.center.x,p.center.y);
turtle.forward(1);
turtle.turn(90);
turtle.forward(1);
turtle.turn(90);
turtle.forward(1);
turtle.turn(90);
turtle.forward(1);
turtle.forward(0.1);
}

turtle.penUp();

notifyListeners(turtle);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ public double getDiameter() {
}

/**
* Absolute position change, make sure pen is up before move and put pen down after move.
* Absolute position change. Raise the pen before move and lower pen after move.
* @param x absolute x position
* @param y absolute y position
*/
Expand All @@ -163,7 +163,7 @@ public void jumpTo(double x,double y) {
}

/**
* Absolute position change, do not adjust pen status
* Absolute position change, do not change current pen status
* @param x relative x position
* @param y relative y position
*/
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/languages/english.xml
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,7 @@
<string><key>Converter_VoronoiStippling.DotMin</key><value>Min dot size</value></string>
<string><key>Converter_VoronoiStippling.DrawBorders</key><value>Draw borders?</value></string>
<string><key>Converter_VoronoiStippling.Cutoff</key><value>Cutoff</value></string>
<string><key>Converter_Voronoi.ShowCenters</key><value>Show centers</value></string>

<string><key>SandyNoble.title</key><value>Sandy Noble Style</value></string>
<string><key>SandyNoble.rings</key><value>Rings</value></string>
Expand Down

0 comments on commit 8aa3090

Please sign in to comment.