complete recreation of the method drawing the pythagoras Tree.

Still has some bugs, and it won't work recursively for large trees, so it is in a while loop.
This commit is contained in:
Jan Christian Grünhage 2015-02-11 11:41:10 +01:00
parent d8ed505128
commit 66a52a118e
2 changed files with 23 additions and 22 deletions

View file

@ -27,26 +27,27 @@ public class PythagorasTree {
this.turtleState = new TurtleState(turtle); this.turtleState = new TurtleState(turtle);
} }
public void addSubTrees(PythagorasTreeQueue queue) { public static boolean drawNextTree(PythagorasTreeQueue queue) {
turtle.pushState(); PythagorasTree tree = queue.remove();
turtle.right(triangles.getLeftAngle(momentaryDepth));
rightSubTree = new PythagorasTree(variant, triangles, turtle, size * triangles.getRightSizeFactor(momentaryDepth), momentaryDepth + 1, maximalDepth);
queue.add(rightSubTree);
turtle.popState(); tree.turtle.setPenColor(drawColor(tree.momentaryDepth, tree.maximalDepth));
turtle.left(triangles.getRightAngle(momentaryDepth)); tree.turtleState.returnToState(tree.turtle);
turtle.forward(size * triangles.getRightSizeFactor(momentaryDepth)); DrawingTools.drawSquare(tree.turtle, tree.size);
leftSubTree = new PythagorasTree(variant, triangles, turtle, size * triangles.getLeftSizeFactor(momentaryDepth), momentaryDepth + 1, maximalDepth);
queue.add(leftSubTree);
}
public boolean drawPythagorasTree(PythagorasTreeQueue queue) { if (tree.momentaryDepth + 1 >= tree.maximalDepth) return false;
turtle.setPenColor(drawColor(momentaryDepth, maximalDepth));
turtleState.returnToState(turtle); tree.turtle.forward(tree.size);
DrawingTools.drawSquare(turtle, size);
turtle.forward(size); tree.turtle.pushState();
if (momentaryDepth + 1 >= maximalDepth) return false; tree.turtle.right(tree.triangles.getLeftAngle(tree.momentaryDepth));
addSubTrees(queue); tree.rightSubTree = new PythagorasTree(tree.variant, tree.triangles, tree.turtle, tree.size * tree.triangles.getRightSizeFactor(tree.momentaryDepth), tree.momentaryDepth + 1, tree.maximalDepth);
queue.add(tree.rightSubTree);
tree.turtle.popState();
tree.turtle.left(tree.triangles.getRightAngle(tree.momentaryDepth));
tree.turtle.forward(tree.size * tree.triangles.getRightSizeFactor(tree.momentaryDepth));
tree.leftSubTree = new PythagorasTree(tree.variant, tree.triangles, tree.turtle, tree.size * tree.triangles.getLeftSizeFactor(tree.momentaryDepth), tree.momentaryDepth + 1, tree.maximalDepth);
queue.add(tree.leftSubTree);
return true; return true;
} }

View file

@ -9,10 +9,10 @@ public class PythagorasTreeQueue extends LinkedList<PythagorasTree> {
public PythagorasTreeQueue(PythagorasTree.PythagorasTreeVariant variant, PythagorasTriangleList triangles, Turtle turtle, double size, int momentaryDepth, int maximalDepth) { public PythagorasTreeQueue(PythagorasTree.PythagorasTreeVariant variant, PythagorasTriangleList triangles, Turtle turtle, double size, int momentaryDepth, int maximalDepth) {
super(); super();
add(new PythagorasTree(variant, triangles, turtle, size, momentaryDepth, maximalDepth)); add(new PythagorasTree(variant, triangles, turtle, size, momentaryDepth, maximalDepth));
boolean finished = false; boolean running = true;
while (!finished) { while (running) {
PythagorasTree tree = remove(); running = PythagorasTree.drawNextTree(this);
finished = tree.drawPythagorasTree(this);
} }
} }
} }