41 lines
1.2 KiB
Java
41 lines
1.2 KiB
Java
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);
|
|
}
|
|
}
|
|
}
|