Solved Task 3

This commit is contained in:
schrom01 2022-10-17 20:57:14 +02:00
parent 124caff620
commit c8aa16c246
2 changed files with 41 additions and 2 deletions

View File

@ -6,7 +6,7 @@ import org.junit.jupiter.api.BeforeEach;
import java.util.StringTokenizer; import java.util.StringTokenizer;
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
/*
public class ADS4_2_test { public class ADS4_2_test {
HilbertServer hlb; HilbertServer hlb;
Turtle turtle; Turtle turtle;
@ -97,4 +97,3 @@ public class ADS4_2_test {
} }
} }
} }
*/

View File

@ -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);
}
}