Add a class for drawing hilbert curves

This commit is contained in:
Manuel Thalmann 2022-10-11 22:53:20 +02:00
parent 019c56c849
commit 983ef9cf60

View file

@ -0,0 +1,40 @@
package ch.nuth.zhaw.exbox;
/**
* Provides the functionality to draw hilbert curves.
*/
public class HilbertServer implements CommandExecutor {
@Override
public String execute(String command) {
int depth = Integer.parseInt(command);
Turtle turtle = Turtle.instance();
double dist = 0.8 / (Math.pow(2, depth + 1) - 1);
turtle.reset(0.1, 0.1);
hilbert(turtle, depth, dist, -90);
return turtle.getTrace();
}
private void hilbert(Turtle turtle, int depth, double dist, double angle) {
if (depth > 0) {
turtle.turn(-angle);
hilbert(turtle, depth - 1, dist, -angle);
turtle.move(dist);
turtle.turn(angle);
hilbert(turtle, depth - 1, dist, angle);
turtle.move(dist);
hilbert(turtle, depth - 1, dist, angle);
turtle.turn(angle);
turtle.move(dist);
hilbert(turtle, depth - 1, dist, -angle);
turtle.turn(-angle);
} else {
turtle.turn(-angle);
turtle.move(dist);
turtle.turn(angle);
turtle.move(dist);
turtle.turn(angle);
turtle.move(dist);
turtle.turn(-angle);
}
}
}