Recursion with Turtles

Concepts in this slide: A list of all useful functions from the turtle module. Turtle Graphics Recursion with Turtles CS111 Computer Programming De...
Author: Barrie Hart
3 downloads 0 Views 1MB Size
Concepts in this slide: A list of all useful functions from the turtle module.

Turtle Graphics

Recursion with Turtles

CS111 Computer Programming Department of Computer Science Wellesley College

Python has a built-in module named turtle. See the Python turtle module API for details. Use from turtle import * to use these commands: fd(dist) bk(dist) lt(angle) rt(angle) pu() pd() pensize(width) pencolor(color) shape(shp) home() clear() reset() setup(width,height)

turtle moves forward by dist turtle moves backward by dist turtle turns left angle degrees turtle turns right angle degrees (pen up) turtle raises pen in belly (pen down) turtle lower pen in belly sets the thickness of turtle's pen to width sets the color of turtle's pen to color sets the turtle's shape to shp turtle returns to (0,0) (center of screen) delete turtle drawings; no change to turtle's state delete turtle drawings; reset turtle's state create a turtle window of given width and height Turtle Recursion 19-2

A Simple Example with Turtles

Concepts in this slide: The only two commands that draw lines are fd and bk.

from turtle import *

Tk window The turtle module has its own graphics environment that is created when we call the function setup. All drawing happens in it.︎

setup(400,400) fd(100) lt(60) shape('turtle') pencolor('red') fd(150) rt(15) pencolor('blue') bk(100) pu() bk(50) pd() pensize(5) bk(250) pensize(1) home() exitonclick()

Looping Turtles (1) Loops can be used in conjunction with turtles to make interesting designs.

Concepts in this slide: The power of abstraction: one function that creates a myriad of different shapes.

def polygon(numSides, sideLength): """ Draws a polygon with the specified number of sides, each with the specified length. """ Will solve this in the Notebook.︎

polygon(4,100)

polygon(3,100) Turtle Recursion 19-3

polygon(6,60)

polygon(5,75)

polygon(100,3)

polygon(7,50) Turtle Recursion 19-4

Looping Turtles (2)

Spiraling Turtles: A Recursion Example

def polyFlow(numPetals, petalSides, petalLen): """Draws 'flowers' with numPetals arranged around a center point. Each petal is a polygon with petalSides sides of length petalLen. """

Will solve this in the Notebook.︎ polyFlow(7,4,80)

polyFlow(10,5,75)

polyFlow(11,6,60)

spiral(200,90,0.9,10)

Answer this: How would you create these shapes using loops?︎ Recursion makes easier solving certain problems that involve a repeating pattern.︎

spiral(200,72,0.97,10)

spiral(200,121,0.95,15)

Turtle Recursion 19-5

Spiraling Turtles: A Recursion Example def spiral(sideLen, angle, scaleFactor, minLength): """Draw a spiral recursively."""

if sideLen >= minLength: fd(sideLen) lt(angle) spiral(sideLen*scaleFactor, angle, scaleFactor, minLength)

•  • 

• 

• 

spiral(200,95,0.93,10) Turtle Recursion 13-6

spiral(625, 90, 0.8, 250)

sideLen is the length of the current side angle is the amount the turtle turns left to draw the next side scaleFactor is the multiplicative factor (between 0.0 and 1.0) by which to scale the next side minLength is the smallest side length that the turtle will draw

spiral(200,80,0.95,10)

Concepts in this slide: Drawing function call frames helps us follow the execution of recursion.

spiral(625, 90, 0.8, 250) if sideLen >= minLength: fd(sideLen) lt(angle) spiral(sideLen*scaleFactor, angle, scaleFactor, minLength)

400 320 256

625

500

spiral(625, 90, 0.8, 250)

Turtle Recursion 19-7

Turtle Recursion 19-8

spiral(625, 90, 0.8, 250)

spiral(625, 90, 0.8, 250) 625

spiral(625, 90, 0.8, 250) if True: fd(sideLen) lt(angle) spiral(sideLen*scaleFactor, angle, scaleFactor, minLength)

spiral(625, 90, 0.8, 250) if True: fd(625) lt(angle) spiral(sideLen*scaleFactor, angle, scaleFactor, minLength)

Turtle Recursion 19-9

spiral(625, 90, 0.8, 250)

Turtle Recursion 19-1 0

spiral(625, 90, 0.8, 250) 625

625

spiral(625, 90, 0.8, 250) if True: fd(625) lt(90) spiral(sideLen*scaleFactor, angle, scaleFactor, minLength)

spiral(625, 90, 0.8, 250) if True: fd(625) lt(90) spiral(500, 90, 0.8, 250) spiral(500, 90, 0.8, 250) if sideLen >= minLength: fd(sideLen) lt(angle) spiral(sideLen*scaleFactor, angle, scaleFactor, minLength)

Turtle Recursion 19-1 1

Turtle Recursion 19-1 2

spiral(625, 90, 0.8, 250)

500

spiral(625, 90, 0.8, 250) 625

625

spiral(625, 90, 0.8, 250) if True: fd(625) lt(90) spiral(500, 90, 0.8, 250)

spiral(625, 90, 0.8, 250) if True: fd(625) lt(90) spiral(500, 90, 0.8, 250)

spiral(500, 90, 0.8, 250) if True: fd(sideLen) lt(angle) spiral(sideLen*scaleFactor, angle, scaleFactor, minLength)

spiral(500, 90, 0.8, 250) if True: fd(500) lt(angle) spiral(sideLen*scaleFactor, angle, scaleFactor, minLength)

Turtle Recursion 19-1 3

500

spiral(625, 90, 0.8, 250)

Turtle Recursion 19-1 4

500

spiral(625, 90, 0.8, 250)

625

625

spiral(625, 90, 0.8, 250) if True: fd(625) lt(90) spiral(500, 90, 0.8, 250)

spiral(625, 90, 0.8, 250) if True: fd(625) lt(90) spiral(500, 90, 0.8, 250)

spiral(500, 90, 0.8, 250) if True: fd(500) lt(90) spiral(sideLen*scaleFactor, angle, scaleFactor, minLength)

spiral(500, 90, 0.8, 250) if True: fd(500) lt(90) spiral(400, 90, 0.8, 250)

Turtle Recursion 19-1 5

spiral(400, 90, 0.8, 250) if sideLen >= minLength: fd(sideLen) lt(angle) spiral(sideLen*scaleFactor, angle, scaleFactor, minLength)

Turtle Recursion 19-1 6

400

500

spiral(625, 90, 0.8, 250) 625

625

spiral(625, 90, 0.8, 250) if True: fd(625) lt(90) spiral(500, 90, 0.8, 250)

spiral(625, 90, 0.8, 250) if True: fd(625) lt(90) spiral(500, 90, 0.8, 250)

spiral(500, 90, 0.8, 250) if True: fd(500) lt(90) spiral(400, 90, 0.8, 250)

spiral(500, 90, 0.8, 250) if True: fd(500) lt(90) spiral(400, 90, 0.8, 250)

spiral(400, 90, 0.8, 250) if True: fd(sideLen) lt(angle) spiral(sideLen*scaleFactor, angle, scaleFactor, minLength)

spiral(400, 90, 0.8, 250) if True: fd(400) lt(angle) spiral(sideLen*scaleFactor, angle, scaleFactor, minLength)

Turtle Recursion 19-1 7

400

Turtle Recursion 19-1 8

400

500

spiral(625, 90, 0.8, 250)

500

spiral(625, 90, 0.8, 250)

500

spiral(625, 90, 0.8, 250)

625

625

spiral(625, 90, 0.8, 250) if True: fd(625) lt(90) spiral(500, 90, 0.8, 250)

spiral(625, 90, 0.8, 250) if True: fd(625) lt(90) spiral(500, 90, 0.8, 250)

spiral(500, 90, 0.8, 250) if True: fd(500) lt(90) spiral(400, 90, 0.8, 250)

spiral(500, 90, 0.8, 250) if True: fd(500) lt(90) spiral(400, 90, 0.8, 250)

spiral(400, 90, 0.8, 250) if True: fd(400) lt(90) spiral(sideLen*scaleFactor, angle, scaleFactor, minLength)

spiral(400, 90, 0.8, 250) if True: fd(400) lt(90) spiral(320, 90, 0.8, 250)

Turtle Recursion

19-19

spiral(320, 90, 0.8, 250) if sideLen >= minLength: fd(sideLen) lt(angle) spiral(sideLen*scaleFactor, angle, scaleFactor, minLength) Turtle Recursion 19-20

400

400 320 500

spiral(625, 90, 0.8, 250)

500

spiral(625, 90, 0.8, 250)

625

625

spiral(625, 90, 0.8, 250) if True: fd(625) lt(90) spiral(500, 90, 0.8, 250)

spiral(625, 90, 0.8, 250) if True: fd(625) lt(90) spiral(500, 90, 0.8, 250)

spiral(500, 90, 0.8, 250) if True: fd(500) lt(90) spiral(400, 90, 0.8, 250)

spiral(500, 90, 0.8, 250) if True: fd(500) lt(90) spiral(400, 90, 0.8, 250)

spiral(400, 90, 0.8, 250) if True: fd(400) lt(90) spiral(320, 90, 0.8, 250)

spiral(320, 90, 0.8, 250) if True: fd(sideLen) lt(angle) spiral(sideLen*scaleFactor, angle, scaleFactor, minLength) Turtle Recursion 19-21

spiral(400, 90, 0.8, 250) if True: fd(400) lt(90) spiral(320, 90, 0.8, 250)

spiral(320, 90, 0.8, 250) if True: fd(320) lt(angle) spiral(sideLen*scaleFactor, angle, scaleFactor, minLength) Turtle Recursion 19-22

400

400

320

320 500

spiral(625, 90, 0.8, 250)

500

spiral(625, 90, 0.8, 250)

625

625

spiral(625, 90, 0.8, 250) if True: fd(625) lt(90) spiral(500, 90, 0.8, 250)

spiral(625, 90, 0.8, 250) if True: fd(625) lt(90) spiral(500, 90, 0.8, 250)

spiral(500, 90, 0.8, 250) if True: fd(500) lt(90) spiral(400, 90, 0.8, 250)

spiral(500, 90, 0.8, 250) if True: fd(500) lt(90) spiral(400, 90, 0.8, 250)

spiral(256, 90, 0.8, 250) if sideLen >= minLength: fd(sideLen) lt(angle) spiral(sideLen*scaleFactor, angle, scaleFactor, minLength)

spiral(400, 90, 0.8, 250) if True: fd(400) lt(90) spiral(320, 90, 0.8, 250)

spiral(320, 90, 0.8, 250) if True: fd(320) lt(90) spiral(256, 90, 0.8, 250) Turtle Recursion 19-24

spiral(400, 90, 0.8, 250) if True: fd(400) lt(90) spiral(320, 90, 0.8, 250)

spiral(320, 90, 0.8, 250) if True: fd(320) lt(90) spiral(sideLen*scaleFactor, angle, scaleFactor, minLength) Turtle Recursion 19-23

400

400

320

320 500

spiral(625, 90, 0.8, 250)

spiral(625, 90, 0.8, 250)

256

625

500

625

spiral(625, 90, 0.8, 250) if True: fd(625) lt(90) spiral(500, 90, 0.8, 250)

spiral(625, 90, 0.8, 250) if True: fd(625) lt(90) spiral(500, 90, 0.8, 250)

spiral(500, 90, 0.8, 250) if True: fd(500) lt(90) spiral(400, 90, 0.8, 250)

spiral(256, 90, 0.8, 250) if True: fd(sideLen) lt(angle) spiral(sideLen*scaleFactor, angle, scaleFactor, minLength)

spiral(500, 90, 0.8, 250) if True: fd(500) lt(90) spiral(400, 90, 0.8, 250)

spiral(256, 90, 0.8, 250) if True: fd(256) lt(angle) spiral(sideLen*scaleFactor, angle, scaleFactor, minLength)

spiral(400, 90, 0.8, 250) if True: fd(400) lt(90) spiral(320, 90, 0.8, 250)

spiral(320, 90, 0.8, 250) if True: fd(320) lt(90) spiral(256, 90, 0.8, 250) Turtle Recursion 19-25

spiral(400, 90, 0.8, 250) if True: fd(400) lt(90) spiral(320, 90, 0.8, 250)

spiral(320, 90, 0.8, 250) if True: fd(320) lt(90) spiral(256, 90, 0.8, 250) Turtle Recursion 19-26

400

400

320

spiral(625, 90, 0.8, 250)

320 256

500

spiral(625, 90, 0.8, 250)

625 spiral(625, 90, 0.8, 250) if True: fd(625) lt(90) spiral(500, 90, 0.8, 250)

256

500

625 spiral(625, 90, 0.8, 250) if True: fd(625) lt(90) spiral(500, 90, 0.8, 250)

spiral(204.8, 90, 0.8, 250) if sideLen >= minLength: fd(sideLen) lt(angle) spiral(sideLen*scaleFactor, angle, scaleFactor, minLength)

spiral(500, 90, 0.8, 250) if True: fd(500) lt(90) spiral(400, 90, 0.8, 250)

spiral(256, 90, 0.8, 250) if True: fd(256) lt(90) spiral(sideLen*scaleFactor, angle, scaleFactor, minLength)

spiral(500, 90, 0.8, 250) if True: fd(500) lt(90) spiral(400, 90, 0.8, 250)

spiral(256, 90, 0.8, 250) if True: fd(256) lt(90) spiral(204.8, 90, 0.8, 250)

spiral(400, 90, 0.8, 250) if True: fd(400) lt(90) spiral(320, 90, 0.8, 250)

spiral(320, 90, 0.8, 250) if True: fd(320) lt(90) spiral(256, 90, 0.8, 250) Turtle Recursion 19-27

spiral(400, 90, 0.8, 250) if True: fd(400) lt(90) spiral(320, 90, 0.8, 250)

spiral(320, 90, 0.8, 250) if True: fd(320) lt(90) spiral(256, 90, 0.8, 250) Turtle Recursion 19-28

400

Substitute computed values (not expressions) to show call frame model of this invocation.

320

spiral(625, 90, 0.8, 250)

256

320

500

256

spiral(625, 90, 0.8, 250)

625 spiral(625, 90, 0.8, 250) if True: fd(625) lt(90) spiral(500, 90, 0.8, 250)

spiral(204.8, 90, 0.8, 250) if False: fd(sideLen) lt(angle) spiral(sideLen*scaleFactor, angle, scaleFactor, minLength)

spiral(500, 90, 0.8, 250) if True: fd(500) lt(90) spiral(400, 90, 0.8, 250)

spiral(256, 90, 0.8, 250) if True: fd(256) lt(90) spiral(204.8, 90, 0.8, 250)

spiral(400, 90, 0.8, 250) if True: fd(400) lt(90) spiral(320, 90, 0.8, 250)

spiral(320, 90, 0.8, 250) if True: fd(320) lt(90) spiral(256, 90, 0.8, 250) Turtle Recursion 19-29

1

2

3

spiral(625, 90, 0.8, 250) if True: fd(625) lt(90) spiral(500, 90, 0.8, 250)

sideLen 625

625

spiral( , 90, 0.8, 250) sideLen if False: fd(sideLen) angle lt(angle) spiral(sideLen*scaleFactor, angle, scaleFactor, minLength)

6

angle 90

spiral(500, 90, 0.8, 250) if sideLen >= minLength: sideLen fd(sideLen) angle lt(angle) spiral(sideLen*scaleFactor, angle, scaleFactor, minLength) spiral( , 90, 0.8, 250 ) if True : sideLen fd(400 ) angle lt(90 ) spiral(320 , 90 0.8 , 250 )

,

spiral(256, 90, 0.8, 250) if True : sideLen fd(256 ) angle lt(90 ) spiral(204.8 , 90 0.8 , 250 )

5

4

400

320

320 256

500

spiral(625, 90, 0.8, 250)

625 spiral(625, 90, 0.8, 250) if True: fd(625) lt(90) spiral(500, 90, 0.8, 250)

,

spiral(320, 90, 0.8, 250) if True : sideLen fd(320 ) angle lt(90 ) spiral(256 , 90 , 0.8 , 250 ) Turtle Recursion 13-30

400

spiral(625, 90, 0.8, 250)

500

Be turtle, draw.

400

Important Initially all execution frames co-exist in the memory. Only once a function has returned (implicitly), the execution frame is deleted. ︎

256

500

625 spiral(625, 90, 0.8, 250) if True: fd(625) lt(90) spiral(500, 90, 0.8, 250)

spiral(500, 90, 0.8, 250) if True: fd(500) lt(90) spiral(400, 90, 0.8, 250)

spiral(256, 90, 0.8, 250) if True: fd(256) lt(90) spiral(204.8, 90, 0.8, 250)

spiral(500, 90, 0.8, 250) if True: fd(500) lt(90) spiral(400, 90, 0.8, 250)

spiral(400, 90, 0.8, 250) if True: fd(400) lt(90) spiral(320, 90, 0.8, 250)

spiral(320, 90, 0.8, 250) if True: fd(320) lt(90) spiral(256, 90, 0.8, 250) Turtle Recursion 19-31

spiral(400, 90, 0.8, 250) if True: fd(400) lt(90) spiral(320, 90, 0.8, 250)

spiral(320, 90, 0.8, 250) if True: fd(320) lt(90) spiral(256, 90, 0.8, 250) Turtle Recursion 19-32

400

400

320

spiral(625, 90, 0.8, 250)

320 256

500

spiral(625, 90, 0.8, 250)

256

625

625

spiral(625, 90, 0.8, 250) if True: fd(625) lt(90) spiral(500, 90, 0.8, 250)

spiral(625, 90, 0.8, 250) if True: fd(625) lt(90) spiral(500, 90, 0.8, 250)

spiral(500, 90, 0.8, 250) if True: fd(500) lt(90) spiral(400, 90, 0.8, 250)

spiral(500, 90, 0.8, 250) if True: fd(500) lt(90) spiral(400, 90, 0.8, 250)

spiral(400, 90, 0.8, 250) if True: fd(400) lt(90) spiral(320, 90, 0.8, 250)

Turtle Recursion 19-33

Turtle Recursion 19-34

400

400

320

spiral(625, 90, 0.8, 250)

500

320 256

500

625

spiral(625, 90, 0.8, 250)

256

500

625

spiral(625, 90, 0.8, 250) if True: fd(625) lt(90) spiral(500, 90, 0.8, 250)

Important All execution frames were one by one deleted after their completion. This terminates the invocation of the function and has created as a “side-effect” the turtle image at the top of the slide.︎

Turtle Recursion 19-35

Turtle Recursion 19-36

Invariant Spiraling

Turtle

A function is invariant relative to an object’s state if the state of the object is the same before and after the function is invoked.

heading x y

90.0

penIsDown

True

43.0 79.0

def spiralBack(sideLen, angle, scaleFactor, minLength): """ Draws a spiral. The state of the turtle (position, color, heading, etc.) after drawing the spiral is the same as before drawing the spiral. """

Turtle Recursion 19-37

Essence of Invariance Do Do … Do Do

How does spiralBack work?

320 256

spiralBack(625, 90, 0.8, 250)

500

625

Be turtle, draw.

400

spiralBack(625, 90, 0.8, 250) if True: sideLen 625 angle 90 fd(625) lt(90) spiralBack(500, 90, 0.8, 250) rt(angle) bk(sideLen)

spiralBack(204.8, 90, 0.8, 250) if False: sideLen 204.8 angle 90 fd(sideLen) lt(angle) spiralBack(sideLen*scaleFactor,…) rt(angle) bk(sideLen)

spiralBack(500, 90, 0.8, 250) if True: sideLen 500 angle 90 fd(500) lt(90) spiralBack(400, 90, 0.8, 250) rt(angle) bk(sideLen)

spiralBack(256, 90, 0.8, 250) if True: sideLen 256 angle 90 fd(256) lt(90) spiralBack(204.8, 90, 0.8, 250) rt(angle) bk(sideLen)

spiralBack(400, 90, 0.8, 250) if True: sideLen 400 angle 90 fd(400) lt(90) spiralBack(320, 90, 0.8, 250) rt(angle) bk(sideLen)

spiralBack(320, 90, 0.8, 250) if True: sideLen 320 angle 90 fd(320) lt(90) spiralBack(256, 90, 0.8, 250) rt(angle) 19-38 bk(sideLen) Turtle Recursion

Trees

state change 1 state change 2

Perform changes to state state change n-1 state change n tree(7, 75, 30, 0.8)

tree(7, 75, 15, 0.8)

Recursive call to function Undo Undo … Undo Undo

state change n state change n-1 state change 2 state change 1

Undo state changes in opposite order tree(10, 80, 45, 0.7) Turtle Recursion 19-39

tree(10, 100, 90, 0.68) Turtle Recursion 19-40

Draw a tree recursively

How to make a 4-level tree: tree(4, 100, 45, 0.6)

tree(levels, trunkLen, angle, shrinkFactor)

and two 3-level trees with 60% trunks set at 45°angles

•  levels is the number of branches on any path from the root

Step 2

to a leaf •  trunkLen is the length of the base trunk of the tree •  angle is the angle from the trunk for each subtree •  shrinkFactor is the shrinking factor for each subtree

Step 1

Make a trunk of size 100

Turtle Recursion 19-41

and two 2-level trees with 60% trunks set at 45°angles

Turtle Recursion 19-42

How to make a 3 level tree: tree(3, 60, 45, 0.6)

How to make a 3-level tree: tree(3, 60, 45, 0.6)

and two level 2 trees with 60% trunks set at 45°angles

Make a trunk of size 60

Make a trunk of size 36

Make a trunk of size 21.6

and two 1-level trees with 60% trunks set at 45°angles

How to make a 2-level tree: tree(2, 36, 45, 0.6)

and two 0-level trees set at 45°angles

Do nothing!

How to make a 1-level tree: tree(1, 21.6, 45, 0.6)

Make a trunk of size 60

How to make a 0-level tree: tree(0, 12.96, 45, 0.6) Turtle Recursion

19-43

Turtle Recursion 13-44

How to make a 2 level tree: tree(2, 36, 45, 0.6)

How to make a 1 level tree: tree(1, 21.6, 45, 0.6)

and two level 1 trees With 60% trunks set at 45°angles

and two level 0 trees set at 45°angles

Make a trunk of size 21.6

Make a trunk of size 36

Turtle Recursion 13-45

Turtle Recursion 13-46

def tree(levels, trunkLen, angle, shrinkFactor): """Draw a 2-branch tree recursively.

How to make a 0 level tree: tree(0, 12.96, 45, 0.6)

Do nothing!

Turtle Recursion 13-47

levels: number of branches on any path from the root to a leaf trunkLen: length of the base trunk of the tree angle: angle from the trunk for each subtree shrinkFactor: shrinking factor for each subtree """ if 0 < levels: # Draw the trunk. fd(trunkLen) # Turn and draw the right subtree. rt(angle) tree(levels-1, trunkLen*shrinkFactor, angle, shrinkFactor) # Turn and draw the left subtree. lt(angle * 2) tree(levels-1, trunkLen*shrinkFactor, angle, shrinkFactor) # Turn back and back up to root without drawing. rt(angle) pu() bk(trunkLen) pd() Turtle Recursion 19-48

Tracing the invocation of tree(3,60, 45,0.6)

Draw trunk and turn to draw level 2 tree

tree(3,60,45,0.6)

tree(3,60,45,0.6) fd(60) rt(45)

Turtle Recursion 19-49

Begin recursive invocation to draw level 2 tree

Turtle Recursion 19-50

Draw trunk and turn to draw level 1 tree

tree(2,36,45,0.6)

tree(2,36,45,0.6) fd(36) rt(45)

tree(3,60,45,0.6)

tree(3,60,45,0.6)

fd(60) rt(45) tree(2,36,45,0.6)

fd(60) rt(45) tree(2,36,45,0.6)

Turtle Recursion 19-51

Turtle Recursion 19-52

Begin recursive invocation to draw level 1 tree

Draw trunk and turn to draw level 0 tree

tree(1,21.6,45,0.6)

tree(1,21.6,45,0.6) fd(21.6) rt(45)

tree(2,36,45,0.6)

tree(2,36,45,0.6)

fd(36) rt(45) tree(1,21.6,45,0.6)

fd(36) rt(45) tree(1,21.6,45,0.6)

tree(3,60,45,0.6)

tree(3,60,45,0.6)

fd(60) rt(45) tree(2,36,45,0.6)

fd(60) rt(45) tree(2,36,45,0.6)

Turtle Recursion 19-53

Begin recursive invocation to draw level 0 tree

Turtle Recursion 19-54

Complete level 0 tree and turn to draw another level 0 tree

tree(1,21.6,45,0.6)

tree(1,21.6,45,0.6)

fd(21.6) rt(45) tree(0,12.96,45,0.6)

fd(21.6) rt(45) tree(0,12.96,45,0.6) lt(90)

tree(2,36,45,0.6)

tree(2,36,45,0.6)

fd(36) rt(45) tree(1,21.6,45,0.6)

fd(36) rt(45) tree(1,21.6,45,0.6)

tree(3,60,45,0.6)

tree(3,60,45,0.6)

fd(60) rt(45) tree(2,36,45,0.6)

fd(60) rt(45) tree(2,36,45,0.6)

Turtle Recursion 19-55

Turtle Recursion

19-56

Begin recursive invocation to draw level 0 tree

tree(2,36,45,0.6)

Complete level 0 tree and return to starting position of level 1 tree

tree(1,21.6,45,0.6)

tree(1,21.6,45,0.6)

fd(21.6) rt(45) tree(0,12.96,45,0.6) lt(90) tree(0,12.96,45,0.6)

fd(21.6) rt(45) tree(0,12.96,45,0.6) lt(90) tree(0,12.96,45,0.6) rt(45) bk(21.6)

tree(2,36,45,0.6)

fd(36) rt(45) tree(1,21.6,45,0.6)

fd(36) rt(45) tree(1,21.6,45,0.6)

tree(3,60,45,0.6)

tree(3,60,45,0.6)

fd(60) rt(45) tree(2,36,45,0.6)

fd(60) rt(45) tree(2,36,45,0.6)

Turtle Recursion 19-57

Complete level 1 tree and turn to draw another level 1 tree

tree(2,36,45,0.6) fd(36) rt(45) tree(1,21.6,45,0.6) lt(90)

Turtle Recursion 19-58

Begin recursive invocation to draw level 1 tree

tree(1,21.6,45,0.6)

tree(1,21.6,45,0.6)

fd(21.6) rt(45) tree(0,12.96,45,0.6) lt(90) tree(0,12.96,45,0.6) rt(45) bk(21.6)

fd(21.6) rt(45) tree(0,12.96,45,0.6) lt(90) tree(0,12.96,45,0.6) rt(45) bk(21.6)

tree(2,36,45,0.6) fd(36) rt(45) tree(1,21.6,45,0.6) lt(90) tree(1,21.6,45,0.6)

tree(3,60,45,0.6)

tree(3,60,45,0.6)

fd(60) rt(45) tree(2,36,45,0.6)

fd(60) rt(45) tree(2,36,45,0.6)

Turtle Recursion 19-59

tree(1,21.6,45,0.6)

Turtle Recursion 19-60

Draw trunk and turn to draw level 0 tree

tree(2,36,45,0.6) fd(36) rt(45) tree(1,21.6,45,0.6) lt(90) tree(1,21.6,45,0.6)

Complete two level 0 trees and return to starting position of level 1 tree tree(1,21.6,45,0.6)

tree(1,21.6,45,0.6)

fd(21.6) rt(45) tree(0,12.96,45,0.6) lt(90) tree(0,12.96,45,0.6) rt(45) bk(21.6)

fd(21.6) rt(45) tree(0,12.96,45,0.6) lt(90) tree(0,12.96,45,0.6) rt(45) bk(21.6)

tree(2,36,45,0.6) fd(36) rt(45) tree(1,21.6,45,0.6) lt(90) tree(1,21.6,45,0.6)

tree(1,21.6,45,0.6) fd(21.6) rt(45)

tree(3,60,45,0.6)

tree(3,60,45,0.6)

fd(60) rt(45) tree(2,36,45,0.6)

fd(60) rt(45) tree(2,36,45,0.6)

tree(1,21.6,45,0.6) fd(21.6) rt(45) tree(0,12.96,45,0.6) lt(90) tree(0,12.96,45,0.6) rt(45) bk(21.6)

Turtle Recursion 19-61

Complete level 1 tree and return to starting position of level 2 tree

tree(2,36,45,0.6) fd(36) rt(45) tree(1,21.6,45,0.6) lt(90) tree(1,21.6,45,0.6) rt(45) bk(36) tree(3,60,45,0.6) fd(60) rt(45) tree(2,36,45,0.6)

Turtle Recursion 19-62

Complete level 2 tree and turn to draw another level 2 tree

tree(1,21.6,45,0.6)

tree(1,21.6,45,0.6)

fd(21.6) rt(45) tree(0,12.96,45,0.6) lt(90) tree(0,12.96,45,0.6) rt(45) bk(21.6)

fd(21.6) rt(45) tree(0,12.96,45,0.6) lt(90) tree(0,12.96,45,0.6) rt(45) bk(21.6)

tree(2,36,45,0.6) fd(36) rt(45) tree(1,21.6,45,0.6) lt(90) tree(1,21.6,45,0.6) rt(45) bk(36)

tree(1,21.6,45,0.6) fd(21.6) rt(45) tree(0,12.96,45,0.6) lt(90) tree(0,12.96,45,0.6) rt(45) bk(21.6)

tree(3,60,45,0.6) fd(60) rt(45) tree(2,36,45,0.6) lt(90)

Turtle Recursion 19-63

tree(1,21.6,45,0.6) fd(21.6) rt(45) tree(0,12.96,45,0.6) lt(90) tree(0,12.96,45,0.6) rt(45) bk(21.6)

Turtle Recursion 19-64

Draw trunk and turn to draw level 1 tree

tree(2,36,45,0.6) fd(36) rt(45) tree(1,21.6,45,0.6) lt(90) tree(1,21.6,45,0.6) rt(45) bk(36) tree(3,60,45,0.6) fd(60) rt(45) tree(2,36,45,0.6) lt(90) tree(2,36,45,0.6)

Draw trunk and turn to draw level 0 tree tree(1,21.6,45,0.6)

tree(1,21.6,45,0.6)

fd(21.6) rt(45) tree(0,12.96,45,0.6) lt(90) tree(0,12.96,45,0.6) rt(45) bk(21.6)

fd(21.6) rt(45) tree(0,12.96,45,0.6) lt(90) tree(0,12.96,45,0.6) rt(45) bk(21.6)

tree(2,36,45,0.6) fd(36) rt(45) tree(1,21.6,45,0.6) lt(90) tree(1,21.6,45,0.6) rt(45) bk(36)

tree(1,21.6,45,0.6) fd(21.6) rt(45) tree(0,12.96,45,0.6) lt(90) tree(0,12.96,45,0.6) rt(45) bk(21.6)

tree(3,60,45,0.6) fd(60) rt(45) tree(2,36,45,0.6) lt(90) tree(2,36,45,0.6)

tree(1,21.6,45,0.6) fd(21.6) rt(45) tree(0,12.96,45,0.6) lt(90) tree(0,12.96,45,0.6) rt(45) bk(21.6) tree(1,21.6,45,0.6) fd(21.6) rt(45)

tree(2,36,45,0.6)

tree(2,36,45,0.6)

fd(36) rt(45)

fd(36) rt(45) tree(1,21.6,45,0.6)

Turtle Recursion 19-65

Complete two level 0 trees and return to starting position of level 1 tree

tree(2,36,45,0.6) fd(36) rt(45) tree(1,21.6,45,0.6) lt(90) tree(1,21.6,45,0.6) rt(45) bk(36) tree(3,60,45,0.6) fd(60) rt(45) tree(2,36,45,0.6) lt(90) tree(2,36,45,0.6)

fd(36) rt(45) tree(1,21.6,45,0.6)

Complete level 1 tree and turn to draw another level 1 tree

tree(1,21.6,45,0.6)

tree(1,21.6,45,0.6)

fd(21.6) rt(45) tree(0,12.96,45,0.6) lt(90) tree(0,12.96,45,0.6) rt(45) bk(21.6)

fd(21.6) rt(45) tree(0,12.96,45,0.6) lt(90) tree(0,12.96,45,0.6) rt(45) bk(21.6)

tree(2,36,45,0.6) fd(36) rt(45) tree(1,21.6,45,0.6) lt(90) tree(1,21.6,45,0.6) rt(45) bk(36)

tree(1,21.6,45,0.6) fd(21.6) rt(45) tree(0,12.96,45,0.6) lt(90) tree(0,12.96,45,0.6) rt(45) bk(21.6)

tree(3,60,45,0.6) fd(60) rt(45) tree(2,36,45,0.6) lt(90) tree(2,36,45,0.6)

tree(1,21.6,45,0.6)

tree(2,36,45,0.6)

Turtle Recursion 19-66

fd(21.6) rt(45) tree(0,12.96,45,0.6) lt(90) tree(0,12.96,45,0.6) rt(45) bk(21.6)

fd(21.6) rt(45) tree(0,12.96,45,0.6) lt(90) tree(0,12.96,45,0.6) rt(45) bk(21.6) tree(1,21.6,45,0.6)

tree(2,36,45,0.6) fd(36) rt(45) tree(1,21.6,45,0.6) lt(90)

Turtle Recursion 19-67

tree(1,21.6,45,0.6)

fd(21.6) rt(45) tree(0,12.96,45,0.6) lt(90) tree(0,12.96,45,0.6) rt(45) bk(21.6)

Turtle Recursion 19-68

Complete two level 0 trees and return to starting position of level 1 tree

Draw trunk and turn to draw level 0 tree

tree(2,36,45,0.6) fd(36) rt(45) tree(1,21.6,45,0.6) lt(90) tree(1,21.6,45,0.6) rt(45) bk(36) tree(3,60,45,0.6) fd(60) rt(45) tree(2,36,45,0.6) lt(90) tree(2,36,45,0.6)

tree(1,21.6,45,0.6)

tree(1,21.6,45,0.6)

fd(21.6) rt(45) tree(0,12.96,45,0.6) lt(90) tree(0,12.96,45,0.6) rt(45) bk(21.6)

fd(21.6) rt(45) tree(0,12.96,45,0.6) lt(90) tree(0,12.96,45,0.6) rt(45) bk(21.6)

tree(2,36,45,0.6) fd(36) rt(45) tree(1,21.6,45,0.6) lt(90) tree(1,21.6,45,0.6) rt(45) bk(36)

tree(1,21.6,45,0.6) fd(21.6) rt(45) tree(0,12.96,45,0.6) lt(90) tree(0,12.96,45,0.6) rt(45) bk(21.6)

tree(3,60,45,0.6) fd(60) rt(45) tree(2,36,45,0.6) lt(90) tree(2,36,45,0.6)

tree(1,21.6,45,0.6)

tree(2,36,45,0.6) fd(36) rt(45) tree(1,21.6,45,0.6) lt(90) tree(1,21.6,45,0.6)

fd(21.6) rt(45) tree(0,12.96,45,0.6) lt(90) tree(0,12.96,45,0.6) rt(45) bk(21.6)

fd(36) rt(45) tree(1,21.6,45,0.6) lt(90) tree(1,21.6,45,0.6)

fd(21.6) rt(45)

Turtle Recursion 19-69

fd(36) rt(45) tree(1,21.6,45,0.6) lt(90) tree(1,21.6,45,0.6) rt(45) bk(36) tree(3,60,45,0.6) fd(60) rt(45) tree(2,36,45,0.6) lt(90) tree(2,36,45,0.6)

fd(36) rt(45) tree(1,21.6,45,0.6) lt(90) tree(1,21.6,45,0.6) rt(45) bk(36)

tree(1,21.6,45,0.6) fd(21.6) rt(45) tree(0,12.96,45,0.6) lt(90) tree(0,12.96,45,0.6) rt(45) bk(21.6)

tree(1,21.6,45,0.6)

tree(1,21.6,45,0.6)

fd(21.6) rt(45) tree(0,12.96,45,0.6) lt(90) tree(0,12.96,45,0.6) rt(45) bk(21.6)

fd(21.6) rt(45) tree(0,12.96,45,0.6) lt(90) tree(0,12.96,45,0.6) rt(45) bk(21.6)

tree(2,36,45,0.6) fd(36) rt(45) tree(1,21.6,45,0.6) lt(90) tree(1,21.6,45,0.6) rt(45) bk(36)

tree(1,21.6,45,0.6) fd(21.6) rt(45) tree(0,12.96,45,0.6) lt(90) tree(0,12.96,45,0.6) rt(45) bk(21.6)

tree(3,60,45,0.6) fd(60) rt(45) tree(2,36,45,0.6) lt(90) tree(2,36,45,0.6) rt(45) bk(60)

tree(1,21.6,45,0.6)

tree(2,36,45,0.6)

fd(21.6) rt(45) tree(0,12.96,45,0.6) lt(90) tree(0,12.96,45,0.6) rt(45) bk(21.6)

fd(21.6) rt(45) tree(0,12.96,45,0.6) lt(90) tree(0,12.96,45,0.6) rt(45) bk(21.6)

Turtle Recursion 19-71

tree(1,21.6,45,0.6) fd(21.6) rt(45) tree(0,12.96,45,0.6) lt(90) tree(0,12.96,45,0.6) rt(45) bk(21.6) tree(1,21.6,45,0.6)

tree(2,36,45,0.6) fd(36) rt(45) tree(1,21.6,45,0.6) lt(90) tree(1,21.6,45,0.6) rt(45) bk(36)

tree(1,21.6,45,0.6) fd(21.6) rt(45) tree(0,12.96,45,0.6) lt(90) tree(0,12.96,45,0.6) rt(45) bk(21.6)

Turtle Recursion 19-70

Complete level 2 tree and return to starting position of level 3 tree

Complete level 1 tree and return to starting position of level 2 tree

tree(2,36,45,0.6)

fd(21.6) rt(45) tree(0,12.96,45,0.6) lt(90) tree(0,12.96,45,0.6) rt(45) bk(21.6) tree(1,21.6,45,0.6)

tree(2,36,45,0.6)

tree(1,21.6,45,0.6)

tree(1,21.6,45,0.6)

fd(21.6) rt(45) tree(0,12.96,45,0.6) lt(90) tree(0,12.96,45,0.6) rt(45) bk(21.6) tree(1,21.6,45,0.6) fd(21.6) rt(45) tree(0,12.96,45,0.6) lt(90) tree(0,12.96,45,0.6) rt(45) bk(21.6)

Turtle Recursion 19-72

Trace the invocation of tree(3, 60, 45, 0.6)

2

fd(21.6) rt(45) tree(0,12.96,45,0.6) lt(90) tree(0,12.96,45,0.6) rt(45) bk(21.6)

tree(2,36,45,0.6) fd(36) rt(45) tree(1,21.6,45,0.6) lt(90) tree(1,21.6,45,0.6) rt(45) bk(36)

1

tree(1,21.6,45,0.6)

3

4

tree(3,60,45,0.6) fd(60) rt(45) tree(2,36,45,0.6) lt(90) tree(2,36,45,0.6) rt(45) bk(60)

5

6

tree(2,36,45,0.6) fd(36) rt(45) tree(1,21.6,45,0.6) lt(90) tree(1,21.6,45,0.6) rt(45) bk(36)

7

The squirrels aren't fooled… Be the turtle, draw the tree, label trunks with i .

tree(1,21.6,45,0.6) fd(21.6) rt(45) tree(0,12.96,45,0.6) lt(90) tree(0,12.96,45,0.6) rt(45) bk(21.6) tree(1,21.6,45,0.6) fd(21.6) rt(45) tree(0,12.96,45,0.6) lt(90) tree(0,12.96,45,0.6) rt(45) bk(21.6) tree(1,21.6,45,0.6) fd(21.6) rt(45) tree(0,12.96,45,0.6) lt(90) tree(0,12.96,45,0.6) rt(45) bk(21.6)

Turtle Recursion 19-73

Random Trees

Turtle Recursion 19-74

More resources Full Slides: http://cs111.wellesley.edu/lectures/lecture19

def treeRandom(length, minLength, thickness, minThickness, minAngle, maxAngle, minShrink, maxShrink): if (length < minLength) or (thickness < minThickness): # Base case pass # Do nothing else: angle1 = random.uniform(minAngle, maxAngle) angle2 = random.uniform(minAngle, maxAngle) shrink1 = random.uniform(minShrink, maxShrink) shrink2 = random.uniform(minShrink, maxShrink) pensize(thickness) fd(length) rt(angle1) treeRandom(length*shrink1, minLength, thickness*shrink1, minThickness, minAngle, maxAngle, minShrink, maxShrink) lt(angle1 + angle2) treeRandom(length*shrink2, minLength, thickness*shrink2, minThickness, minAngle, maxAngle, minShrink, maxShrink) rt(angle2) pensize(thickness) bk(length) Turtle Recursion 19-75

•  All steps of recursion examples, drawn out. •  Exercises for drawing Koch curves and snowflakes with

recursive turtle functions. •  History about turtles at Wellesley and elsewhere. •  Applying the turtle programming abstraction to control laser cutters in the WeLab (Wellesley engineering lab).

snowflake(3,150)

4-76

Snowflakes

Drawing fractals – Koch Curve koch(levels, size) koch(0, 150) koch(1, 150)

snowflake(0,150)

snowflake(2,150)

snowflake(1,150)

snowflake(3,150)

koch(2, 150) koch(3, 150) Turtle Recursion 19-77

Turtle Ancestry o 

Turtle Recursion 19-78

Turtle Ancestry (cont d) o  Richard Pattis s Karel the Robot (1981)

Floor turtles used to teach children problem solving in late 1960s. Controlled by LOGO programming language created by Wally Feurzeig (BBN), Daniel Bobrow (BBN), and Seymour Papert (MIT).

teaches problem-solving using Pascal robots that manipulate beepers in a grid world. o  Turtle Geometry book by Andrea diSessa and Hal Abelson (1986). o  LEGO/Logo project at MIT (Mitchel Resnick and Steve Ocko, 1988); evolves into Handyboards (Fred Martin and Brian Silverman), Crickets (Robbie Berg @ Wellesley), and LEGO Mindstorms o  StarLogo – programming with thousands of turtles in Resnick s Turtles, Termites, and Traffic Jams (1997).

o  Logo-based turtles introduced

around 1971 by Papert's MIT Logo Laboratory.

o  Turtles play a key role in

constructionist learning philosophy espoused by Papert in Mindstorms (1980).

Turtle Recursion 19-79

Turtle Recursion 19-80

Turtles, Buggles, & Friends At Wellesley o  o 

o 

o 

o 

o 

In mid-1980s, Eric Roberts teaches programming using software-based turtles. In 1996, Robbie Berg and Lyn Turbak start teaching Robotic Design Studio with Sciborgs. In 1996, Randy Shull and Takis Metaxas use turtles to teach problem solving in CS110. In 1997, BuggleWorld introduced by Lyn Turbak when CS111 switches from Pascal to Java. Turtles are also used in the course In 2006, Robbie Berg and others introduce PICO Crickets: http://www.picocricket.com In 2011, Lyn Turbak and the TinkerBlocks group introduce TurtleBlocks, a blocksbased turtle language whose designs can be turned into physical artifacts with laser and vinyl cutters.

Laser Cutting a Tree with Turtle Blocks

regular mode

boundary mode

laser cutting

Turtle Recursion 19-81

Turtle Recursion 19-82