40 lines
1.1 KiB
Java
40 lines
1.1 KiB
Java
|
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);
|
||
|
}
|
||
|
}
|
||
|
}
|