Log on and Download from website tur Snow
Log on and Download from website: tur. Snow. py, tur. Snow. Depth. py, Recursive. Programmingwith. Turtles. 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 • Two forms of recursion: – As a substitute for looping • Menu program asking for user input, until e. Xit 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 Snowflake specifying 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/3 2. Turn left 60 degrees. 3. Draw Koch curve with length x/3 4. 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)
Recursive Zoom
Comparing Algorithms Depth/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) Length Algorithm What happens if: Length is changed in D/L algorithm? Depth is changed in D/L algorithm? How would you achieve same results in Length algorithm: Length? Depth? 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 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
- Slides: 17