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

17
Log on and Download from website: turSnow.py, turSnowDepth.py, RecursiveProgrammingwithTurtles.pptx

Upload: georgina-greene

Post on 27-Dec-2015

213 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Log on and Download from website: turSnow.py, turSnowDepth.py, RecursiveProgrammingwithTurtles.pptx

Log on and Download from website:turSnow.py,

turSnowDepth.py, RecursiveProgrammingwithTurtles.pptx

Page 2: Log on and Download from website: turSnow.py, turSnowDepth.py, RecursiveProgrammingwithTurtles.pptx

Recursive Programming with Python Turtles

March 30, 2011 ASFA – Programming II

Page 3: Log on and Download from website: turSnow.py, turSnowDepth.py, RecursiveProgrammingwithTurtles.pptx

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

Page 4: Log on and Download from website: turSnow.py, turSnowDepth.py, RecursiveProgrammingwithTurtles.pptx

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

Page 5: Log on and Download from website: turSnow.py, turSnowDepth.py, RecursiveProgrammingwithTurtles.pptx

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

Page 6: Log on and Download from website: turSnow.py, turSnowDepth.py, RecursiveProgrammingwithTurtles.pptx

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

Page 7: Log on and Download from website: turSnow.py, turSnowDepth.py, RecursiveProgrammingwithTurtles.pptx

Recursive Algorithm

Koch Curve

Stages of construction

Page 8: Log on and Download from website: turSnow.py, turSnowDepth.py, RecursiveProgrammingwithTurtles.pptx
Page 9: Log on and Download from website: turSnow.py, turSnowDepth.py, RecursiveProgrammingwithTurtles.pptx

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)

Page 10: Log on and Download from website: turSnow.py, turSnowDepth.py, RecursiveProgrammingwithTurtles.pptx

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.

Page 11: Log on and Download from website: turSnow.py, turSnowDepth.py, RecursiveProgrammingwithTurtles.pptx

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)

Page 12: Log on and Download from website: turSnow.py, turSnowDepth.py, RecursiveProgrammingwithTurtles.pptx

Recursive Zoom

Page 13: Log on and Download from website: turSnow.py, turSnowDepth.py, RecursiveProgrammingwithTurtles.pptx

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?

Page 14: Log on and Download from website: turSnow.py, turSnowDepth.py, RecursiveProgrammingwithTurtles.pptx

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?

Page 15: Log on and Download from website: turSnow.py, turSnowDepth.py, RecursiveProgrammingwithTurtles.pptx

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)

Page 16: Log on and Download from website: turSnow.py, turSnowDepth.py, RecursiveProgrammingwithTurtles.pptx

Some Other Similar Drawings

Page 17: Log on and Download from website: turSnow.py, turSnowDepth.py, RecursiveProgrammingwithTurtles.pptx

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