MCPcopy Create free account
hub / github.com/asweigart/PythonStdioGames / drawBranch

Function drawBranch

src/gamesbyexample/fractaltree.py:12–36  ·  view source on GitHub ↗
(startPosition, direction, branchLength)

Source from the content-addressed store, hash-verified

10turtle.hideturtle()
11
12def 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
38try:
39 seed = 0

Callers 1

fractaltree.pyFile · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected