 |  |

 |
 |  |  |
 | Welcome to CS1315. Click on the python to add comments.
|  |
 |  |  |
|
This page removed for FERPA compliance
|
        |
Recursively drawn trees
# A tree-drawing function demonstrating recursion.
# Adapted from a Logo program in Abelson and Di Sessa's "Turtle Geometry"
# (1980, p. 83)
import time
# Initialize a turtle in the center, near the bottom of the world
def init():
width = 1000
height = 700
world = makeWorld(width, height)
t = makeTurtle(world)
penUp(t)
moveTo(t, int(width/2), int(5*height/6))
penDown(t)
return t
import random
# Recursive function to draw a branch.
def branch(bud, length, level):
# The base case.
if level <= 0:
return
# The recursive case: spawn two branches 45 degrees left and right of heading
# and one third as long as the branch we are currently on
forward(bud, length)
turn(bud, -45)
branch(bud, 2*length/3, level-1)
turn(bud, 90)
branch(bud, 2*length/3, level-1)
turn(bud, -45)
forward(bud, -length)
time.sleep(0.01)
# This is the top-level function.
# Try it with startingREMOVEDngth=100 and levels=8 and experiment from there.
def growTree(startingREMOVEDngth, levels):
b = init()
branch(b, startingREMOVEDngth, levels)
Link to this Page