finished s04

This commit is contained in:
navid.sassan 2020-10-18 23:54:21 +02:00
parent b58db20935
commit 52e9b2f979
2 changed files with 66 additions and 0 deletions
04/src/main/java/ch/zhaw/ads

@ -0,0 +1,31 @@
package ch.zhaw.ads;
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();
}
private void hilbert(int depth, double dist, double angle) {
if (depth >= 0) {
turtle.turn(-angle);
hilbert(depth-1, dist, -angle);
turtle.move(dist);
turtle.turn(angle);
hilbert(depth-1, dist, angle);
turtle.move(dist);
hilbert(depth-1, dist, angle);
turtle.turn(angle);
turtle.move(dist);
hilbert(depth-1, dist, -angle);
turtle.turn(-angle);
}
}
}

@ -0,0 +1,35 @@
package ch.zhaw.ads;
public class SnowflakeServer implements CommandExecutor {
Turtle turtle;
@Override
public String execute(String command) {
int n = Integer.parseInt(command);
turtle = new Turtle();
snowflake(Integer.parseInt(command), 0.8);
turtle.turn(-120);
snowflake(Integer.parseInt(command), 0.8);
turtle.turn(-120);
snowflake(Integer.parseInt(command), 0.8);
return turtle.getTrace();
}
private void snowflake(int stufe, double dist) {
if (stufe == 0) {
turtle.move(dist);
} else {
stufe--;
dist = dist/3;
snowflake(stufe,dist);
turtle.turn(60);
snowflake(stufe,dist);
turtle.turn(-120);
snowflake(stufe,dist);
turtle.turn(60);
snowflake(stufe,dist);
}
}
}