Change Contents of the Bubble
Welcome to CS1315. Click on the python to add comments.

Looking for the book? They have it at the Engineer's Bookstore at 748 Marietta St NW. Here is there website: http://www.engrbookstore.com/ - Monica

Hotspots: Slides and CodeTA CornerComments?AnnouncementsFAQStatic Webspace
View this PageEdit this PageUploads to this PageHistory of this PageHomeRecent ChangesSearchHelp Guide

tree

# 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.5)

# This is the top-level function.
# Try it with startingLength=100 and levels=8 and experiment from there.
def growTree(startingLength, levels):
  b = init()
  branch(b, startingLength, levels)


Link to this Page