From c8aa16c2463e8b9b6b2efeaba9dd44dd9aa2ce93 Mon Sep 17 00:00:00 2001 From: schrom01 Date: Mon, 17 Oct 2022 20:57:14 +0200 Subject: [PATCH] Solved Task 3 --- src/ch/zhaw/ads/ADS4_2_test.java | 3 +-- src/ch/zhaw/ads/HilbertServer.java | 40 ++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 2 deletions(-) create mode 100644 src/ch/zhaw/ads/HilbertServer.java diff --git a/src/ch/zhaw/ads/ADS4_2_test.java b/src/ch/zhaw/ads/ADS4_2_test.java index b753812..3c18892 100644 --- a/src/ch/zhaw/ads/ADS4_2_test.java +++ b/src/ch/zhaw/ads/ADS4_2_test.java @@ -6,7 +6,7 @@ import org.junit.jupiter.api.BeforeEach; import java.util.StringTokenizer; import static org.junit.jupiter.api.Assertions.assertEquals; -/* + public class ADS4_2_test { HilbertServer hlb; Turtle turtle; @@ -97,4 +97,3 @@ public class ADS4_2_test { } } } -*/ \ No newline at end of file diff --git a/src/ch/zhaw/ads/HilbertServer.java b/src/ch/zhaw/ads/HilbertServer.java new file mode 100644 index 0000000..4017f4e --- /dev/null +++ b/src/ch/zhaw/ads/HilbertServer.java @@ -0,0 +1,40 @@ +package ch.zhaw.ads; + +import ch.zhaw.ads.CommandExecutor; +import ch.zhaw.ads.Turtle; + +public class HilbertServer implements CommandExecutor { + Turtle turtle; + + @Override + public String execute(String command) { + int depth = Integer.parseInt(command); + double dist = 0.8 / (Math.pow(2,depth+1)-1); + turtle = new Turtle(0.1, 0.1); + hilbert(depth, dist, -90); + return turtle.getTrace(); + } + + public void hilbert(int depth, double dist, double angle){ + if(depth < 0){ + return; + } + depth --; + turtle.turn(-angle); + // draw recursive + hilbert(depth, dist, -angle); + turtle.move(dist); + turtle.turn(angle); + // draw recursive + hilbert(depth, dist, angle); + turtle.move(dist); + // draw recursive + hilbert(depth, dist, angle); + turtle.turn(angle); + turtle.move(dist); + // draw recursive + hilbert(depth, dist, -angle); + turtle.turn(-angle); + + } +}