finished s04
This commit is contained in:
parent
b58db20935
commit
52e9b2f979
04/src/main/java/ch/zhaw/ads
31
04/src/main/java/ch/zhaw/ads/HilbertServer.java
Normal file
31
04/src/main/java/ch/zhaw/ads/HilbertServer.java
Normal file
@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
35
04/src/main/java/ch/zhaw/ads/SnowflakeServer.java
Normal file
35
04/src/main/java/ch/zhaw/ads/SnowflakeServer.java
Normal file
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user