diff --git a/app/src/main/java/ch/nuth/zhaw/exbox/HilbertServer.java b/app/src/main/java/ch/nuth/zhaw/exbox/HilbertServer.java new file mode 100644 index 0000000..7097855 --- /dev/null +++ b/app/src/main/java/ch/nuth/zhaw/exbox/HilbertServer.java @@ -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); + } + } +}