1 / 17

Log on and Download from website:

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.

enid
Download Presentation

Log on and Download from website:

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  2. Recursive Programming with Python Turtles March 30, 2011 ASFA – Programming II

  3. 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

  4. 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

  5. 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

  6. 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

  7. Recursive Algorithm Koch Curve Stages of construction

  8. 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)

  9. 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.

  10. 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)

  11. Recursive Zoom

  12. 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?

  13. 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?

  14. 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)

  15. Some Other Similar Drawings

  16. 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

More Related