| 10 | turtle.hideturtle() |
| 11 | |
| 12 | def drawBranch(startPosition, direction, branchLength): |
| 13 | if branchLength < 5: |
| 14 | # Base case; the branches are two small to keep drawing more: |
| 15 | return |
| 16 | |
| 17 | # Go to the starting point & direction: |
| 18 | turtle.penup() |
| 19 | turtle.goto(startPosition) |
| 20 | turtle.setheading(direction) |
| 21 | |
| 22 | # Draw the branch (thickness is 1/7 the length): |
| 23 | turtle.pendown() |
| 24 | turtle.pensize(max(branchLength / 7.0, 1)) |
| 25 | turtle.forward(branchLength) |
| 26 | |
| 27 | # Record the position of the branch's end: |
| 28 | endPosition = turtle.position() |
| 29 | leftDirection = direction + LEFT_ANGLE |
| 30 | leftBranchLength = branchLength - LEFT_DECREASE |
| 31 | rightDirection = direction - RIGHT_ANGLE |
| 32 | rightBranchLength = branchLength - RIGHT_DECREASE |
| 33 | |
| 34 | # Recursive case; draw two more branches: |
| 35 | drawBranch(endPosition, leftDirection, leftBranchLength) |
| 36 | drawBranch(endPosition, rightDirection, rightBranchLength) |
| 37 | |
| 38 | try: |
| 39 | seed = 0 |