log on and download from website: tursnow.py, tursnowdepth.py, recursiveprogrammingwithturtles.pptx

Post on 27-Dec-2015

213 Views

Category:

Documents

1 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Log on and Download from website:turSnow.py,

turSnowDepth.py, RecursiveProgrammingwithTurtles.pptx

Recursive Programming with Python Turtles

March 30, 2011 ASFA – Programming II

Remember: Logo Turtle• Dr. Seymour Papert at MIT invented the Turtle as a

graphical and mathematical object to think with for the children’s programming language, Logo (1966)

• Children programmed robot turtles to draw pictures

How Turtles Draw

• Think of a turtle crawling on a piece of paper, with a pen tied to its tailPosition specified with (x, y) coordinates• Cartesian coordinate system, with origin (0, 0) at the

center of a window

Turtles Need to Survive as a Species

• They get tired of just executing simple programs

• They want to “reproduce” themselves• How can they do that?– RECURSION– RECURSION– RECURSION– RECURSION

Recursion

• Two forms of recursion:– As a substitute for looping

• Menu program asking for user input, until eXit selected

– Breaking a problem down into a smaller problem repeatedly until reach some base case• Fibonacci numbers• Factorials • “Martin and the Dragon”

• Definition of a recursive method: a method that calls itself

Recursive Algorithm

Koch Curve

Stages of construction

Drawing a Koch Snowflakespecifying length and depth

from turtle import * def f(length, depth): if depth == 0: forward(length) else: f(length/3, depth-1) right(60) f(length/3, depth-1) left(120) f(length/3, depth-1) right(60) f(length/3, depth-1) f(300, 3)

Alternative AlgorithmTo draw and Koch curve with length 'x‘ :

1. Draw Koch curve with length x/32. Turn left 60degrees.3. Draw Koch curve with length x/34. Turn right 120 degrees.5. Draw Koch curve with length x/3.6. Turn left 60 degrees.7. Draw Koch curve with length x/3.

The base case is when x is less than 2. In that case, you can just draw a straight line with length x.

Alternative in Pythonimport turtle def f(length): turtle.shape("turtle") turtle.speed(10) turtle.color(0,.6,.7) if length <= 2: turtle.forward(length) else: f(length/3) turtle.right(60) f(length/3) turtle.left(120) f(length/3) turtle.right(60) f(length/3) f(200)

Recursive Zoom

Comparing Algorithmsfrom turtle import * def f(length, depth): if depth == 0: forward(length) else: f(length/3, depth-1) right(60) f(length/3, depth-1) left(120) f(length/3, depth-1) right(60) f(length/3, depth-1) f(300, 3)

import turtle def f(length): turtle.shape("turtle") turtle.speed(10) turtle.color(0,.6,.7) if length <= 2: turtle.forward(length) else: f(length/3) turtle.right(60) f(length/3) turtle.left(120) f(length/3) turtle.right(60) f(length/3) f(200)

What happens if:

Length is changed in D/L algorithm?

Depth is changed in D/L algorithm?

Depth/Length Algorithm Length Algorithm

How would you achieve same results in Length algorithm:

Length?

Depth?

How Draw the Entire Snowflake?

• We are only drawing one of the 3 sides from the original triangle.

• How would you draw the entire snowflake?

That’s right just turn and loop 3 times

from turtle import * def f(length, depth): if depth == 0: forward(length) else: f(length/3, depth-1) right(60) f(length/3, depth-1) left(120) f(length/3, depth-1) right(60) f(length/3, depth-1) f(300, 3)

def kflake(size=100,depth=2): for i in range(3): f(size,depth) turtle.left(120) return

kflake(100,2)

Some Other Similar Drawings

Assignment

• Create a recursive turtle drawing of your choosing– You must design it on paper first

• Draw it• Pseudocode it• Code it

– Turn them all in via paper (draw and pseudocode) and email (code)

• Take your time, do something beautiful• Sufficient effort must be evident

top related