From 67d698f5373d8813bc5cd7f911e331eefcf7231f Mon Sep 17 00:00:00 2001 From: Manuel Thalmann Date: Tue, 11 Oct 2022 22:27:54 +0200 Subject: [PATCH] Add classes for drawing snowflakes --- .../ch/nuth/zhaw/exbox/SnowflakeServer.java | 39 ++++++++++++ .../ch/nuth/zhaw/exbox/TestGraphicServer.java | 17 +++++ .../main/java/ch/nuth/zhaw/exbox/Turtle.java | 63 +++++++++++++++++++ 3 files changed, 119 insertions(+) create mode 100644 app/src/main/java/ch/nuth/zhaw/exbox/SnowflakeServer.java create mode 100644 app/src/main/java/ch/nuth/zhaw/exbox/TestGraphicServer.java create mode 100644 app/src/main/java/ch/nuth/zhaw/exbox/Turtle.java diff --git a/app/src/main/java/ch/nuth/zhaw/exbox/SnowflakeServer.java b/app/src/main/java/ch/nuth/zhaw/exbox/SnowflakeServer.java new file mode 100644 index 0000000..30754f2 --- /dev/null +++ b/app/src/main/java/ch/nuth/zhaw/exbox/SnowflakeServer.java @@ -0,0 +1,39 @@ +package ch.nuth.zhaw.exbox; + +/** + * Provides the functionality to draw a snowflake. + */ +public class SnowflakeServer implements CommandExecutor { + @Override + public String execute(String command) { + int recursionLevel = Integer.parseInt(command); + Turtle turtle = Turtle.instance(); + drawSnowflake(turtle, recursionLevel); + return turtle.getTrace(); + } + + public void drawSnowflake(Turtle turtle, int recursionLevel) { + turtle.reset(0.1, 0.266); + turtle.turn(60); + + for (int i = 0; i < 3; i++) { + drawLine(turtle, recursionLevel, 0.8); + turtle.turn(-120); + } + } + + public void drawLine(Turtle turtle, int recursionLevel, double destination) { + if (recursionLevel == 0) { + turtle.move(destination); + } else { + double distance = destination / 3; + + for (int i = 0; i < 3; i++) { + drawLine(turtle, recursionLevel - 1, distance); + turtle.turn(i % 2 == 0 ? 60 : -120); + } + + drawLine(turtle, recursionLevel - 1, distance); + } + } +} diff --git a/app/src/main/java/ch/nuth/zhaw/exbox/TestGraphicServer.java b/app/src/main/java/ch/nuth/zhaw/exbox/TestGraphicServer.java new file mode 100644 index 0000000..fd0c0de --- /dev/null +++ b/app/src/main/java/ch/nuth/zhaw/exbox/TestGraphicServer.java @@ -0,0 +1,17 @@ +package ch.nuth.zhaw.exbox; + +public class TestGraphicServer implements CommandExecutor { + String figure = "\n" + + "\n" + + "\n" + + "\n" + + "\n" + + "\n" + + "\n" + + "\n" + + "\n"; + + public String execute(String command) { + return figure; + } +} diff --git a/app/src/main/java/ch/nuth/zhaw/exbox/Turtle.java b/app/src/main/java/ch/nuth/zhaw/exbox/Turtle.java new file mode 100644 index 0000000..e02d0c9 --- /dev/null +++ b/app/src/main/java/ch/nuth/zhaw/exbox/Turtle.java @@ -0,0 +1,63 @@ +package ch.nuth.zhaw.exbox; + +public class Turtle { + private static StringBuffer b; + private static double x, y; + private static double angle; + private static Turtle theTurtle; + + public static Turtle instance() { + if (theTurtle == null) { + theTurtle = new Turtle(); + } + + return theTurtle; + } + + public Turtle() { + this(0, 0); + } + + public Turtle(double x, double y) { + reset(x, y); + theTurtle = this; + } + + public void reset(double x, double y) { + b = new StringBuffer(); + Turtle.x = x; + Turtle.y = y; + angle = 0; + } + + public void clear() { + reset(0, 0); + } + + public String getTrace() { + return b.toString(); + } + + private double round(double d) { + return Math.round(d * 10000) / 10000.0; + } + + public void move(double dist) { + b.append("\n"); + } + + public void turn(double turnAngle) { + angle += turnAngle * Math.PI / 180; + } +}