170 likes | 299 Views
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.
E N D
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 tail Position 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 Algorithm To 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 Python 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)
Comparing Algorithms Depth/Length Algorithm Length Algorithm 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) What happens if: Length is changed in D/L algorithm? Depth is changed in D/L algorithm? 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) 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) defkflake(size=100,depth=2): for i in range(3): f(size,depth) turtle.left(120) return kflake(100,2)
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