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

Function drawBranch

src/gamesbyexample/fractalnonuniformtree.py:29–66  ·  view source on GitHub ↗
(x, y, direction, branchLength)

Source from the content-addressed store, hash-verified

27
28
29def drawBranch(x, y, direction, branchLength):
30 # if the branch is too small, just quit
31 if branchLength < 5:
32 return
33
34 # Draw the branch:
35 branchThickness = max(branchLength / 7.0, 1) + random.randint(-1, 1)
36 turtle.pensize(branchThickness)
37 for i in range(4):
38 turtle.forward(branchLength / 4.0 + random.randint(-10, 10))
39 turtle.left(random.randint(-8, 8))
40
41 if random.randint(0, 5) == 0:
42 tinyBranchAngle = random.randint(-LEFT_ANGLE, RIGHT_ANGLE)
43 turtle.right(tinyBranchAngle)
44 drawBranch(turtle.xcor(), turtle.ycor(), turtle.heading(), branchLength / 2)
45 turtle.left(tinyBranchAngle)
46 turtle.pensize(branchThickness)
47
48 if random.randint(0, 5) == 0:
49 branchLength = branchLength * 0.9
50
51 # Draw the two recursive branches:
52 if random.randint(0, 9) != 0:
53 turtle.left(LEFT_ANGLE)
54 drawBranch(turtle.xcor(), turtle.ycor(), turtle.heading(), branchLength - LEFT_DECREASE)
55 turtle.right(LEFT_ANGLE)
56
57 if random.randint(0, 9) != 0:
58 turtle.right(RIGHT_ANGLE)
59 drawBranch(turtle.xcor(), turtle.ycor(), turtle.heading(), branchLength - RIGHT_DECREASE)
60 turtle.left(RIGHT_ANGLE)
61
62 # Return back to the starting point:
63 turtle.penup()
64 turtle.goto(x, y)
65 turtle.setheading(direction)
66 turtle.pendown()
67
68def drawTree(x, y, direction, seed):
69 global LEFT_ANGLE, RIGHT_ANGLE, LEFT_DECREASE, RIGHT_DECREASE

Callers 1

drawTreeFunction · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected