Add a class for drawing hilbert curves
This commit is contained in:
parent
019c56c849
commit
983ef9cf60
1 changed files with 40 additions and 0 deletions
40
app/src/main/java/ch/nuth/zhaw/exbox/HilbertServer.java
Normal file
40
app/src/main/java/ch/nuth/zhaw/exbox/HilbertServer.java
Normal file
|
@ -0,0 +1,40 @@
|
|||
package ch.nuth.zhaw.exbox;
|
||||
|
||||
/**
|
||||
* Provides the functionality to draw hilbert curves.
|
||||
*/
|
||||
public class HilbertServer implements CommandExecutor {
|
||||
@Override
|
||||
public String execute(String command) {
|
||||
int depth = Integer.parseInt(command);
|
||||
Turtle turtle = Turtle.instance();
|
||||
double dist = 0.8 / (Math.pow(2, depth + 1) - 1);
|
||||
turtle.reset(0.1, 0.1);
|
||||
hilbert(turtle, depth, dist, -90);
|
||||
return turtle.getTrace();
|
||||
}
|
||||
|
||||
private void hilbert(Turtle turtle, int depth, double dist, double angle) {
|
||||
if (depth > 0) {
|
||||
turtle.turn(-angle);
|
||||
hilbert(turtle, depth - 1, dist, -angle);
|
||||
turtle.move(dist);
|
||||
turtle.turn(angle);
|
||||
hilbert(turtle, depth - 1, dist, angle);
|
||||
turtle.move(dist);
|
||||
hilbert(turtle, depth - 1, dist, angle);
|
||||
turtle.turn(angle);
|
||||
turtle.move(dist);
|
||||
hilbert(turtle, depth - 1, dist, -angle);
|
||||
turtle.turn(-angle);
|
||||
} else {
|
||||
turtle.turn(-angle);
|
||||
turtle.move(dist);
|
||||
turtle.turn(angle);
|
||||
turtle.move(dist);
|
||||
turtle.turn(angle);
|
||||
turtle.move(dist);
|
||||
turtle.turn(-angle);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue