From 983ef9cf60207e88a54f0e84a4c0a4723d28bb4d Mon Sep 17 00:00:00 2001 From: Manuel Thalmann Date: Tue, 11 Oct 2022 22:53:20 +0200 Subject: [PATCH] Add a class for drawing hilbert curves --- .../ch/nuth/zhaw/exbox/HilbertServer.java | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 app/src/main/java/ch/nuth/zhaw/exbox/HilbertServer.java 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); + } + } +}