Recursion with Turtles

Similar documents
Recursion with Turtles

1 Turtle Graphics Concepts

Fractal. Fractals. L- Systems 1/17/12

Python 3 Turtle graphics. Lecture 24 COMPSCI111/111G SS 2017

Code, Draw, and 3D-Print with Turtle Tina

Turtle Ballet: Simulating Parallel Turtles in a Nonparallel LOGO Version. Erich Neuwirth

Package TurtleGraphics

Writing Simple Procedures Drawing a Pentagon Copying a Procedure Commanding PenUp and PenDown Drawing a Broken Line...

Lecture 1: Turtle Graphics. the turtle and the crane and the swallow observe the time of their coming; Jeremiah 8:7

A Guide to the TurtleGraphics Package for R

PYTHON FOR KIDS A Pl ayfu l I ntrodu ctio n to Prog r am m i ng J a s o n R. B r i g g s

Grandparents U, 2018 Part 2

Workbook. Version 3. Created by G. Mullin and D. Carty

The City School. Learn Create Program

Reference Guide Playful Invention Company

Machine Learning.! A completely different way to have an. agent acquire the appropriate abilities to solve a particular goal is via machine learning.

GEOG 490/590 SPATIAL MODELING SPRING 2015 ASSIGNMENT 3: PATTERN-ORIENTED MODELING WITH AGENTS

Finch Robot: snap level 4

Lab 10: Color Sort Turtles not yet sorted by color

Lab 5: Bumper Turtles

Sketch Out the Design

In this project you will use loops to create a racing turtle game and draw a race track.

Half-Lives of Antibiotics

AKC Rally More Advanced Signs

Lab 7: Experimenting with Life and Death

StarLogo Complete Command List (Edited and reformatted by Nicholas Gessler, 6 June 2001.)

~~~***~~~ A Book For Young Programmers On Scratch. ~~~***~~~

CS108L Computer Science for All Module 7: Algorithms

5 State of the Turtles

Scratch Lesson Plan. Part One: Structure. Part Two: Movement

COMPARING DNA SEQUENCES TO UNDERSTAND EVOLUTIONARY RELATIONSHIPS WITH BLAST

International Play Concepts B.V. PO box 29 NL-3890 AA Zeewolde The Netherlands. T: +31(0) E: W:

6. 1 Leaping Lizards!

An Esterel Virtual Machine (EVM) Aruchunan Vaseekaran

Virtual Dog Program in Scratch. By Phil code-it.co.uk

Do the traits of organisms provide evidence for evolution?

COYOTES and FOXES. Final Report. - Chantilly Fulgham, - Gracie Sanchez,

Geometry from Scratch

Maze Game Maker Challenges. The Grid Coordinates

January Review-Cumulative Review Page 1 of 9

A Peek Into the World of Streaming

Introduction to phylogenetic trees and tree-thinking Copyright 2005, D. A. Baum (Free use for non-commercial educational pruposes)

INTRODUCTION. and a hat, a hot pot, a cat hops, a cat in a hat, a fat cat. and Pat are fat, Jat is a big cat, Pat is a little bat.

FPGA-based Emotional Behavior Design for Pet Robot

Econometric Analysis Dr. Sobel

6.836 Embodied Intelligence Final Project: Tom and Jerry. Gleb Chuvpilo, Jessica Howe chuvpilo, May 15, 2002

Coding with Scratch - First Steps

All Dogs Parkour Exercises (Interactions) updated to October 6, 2018

GARNET STATIC SHOCK BARK COLLAR

THE PIGEONHOLE PRINCIPLE AND ITS APPLICATIONS

Help Guide. Locating parts and controls. Getting ready for your life with aibo

Elicia Calhoun Seminar for Mobility Challenged Handlers PART 3

Looking at insects: more keys

Day at the Zoo. Nonstandard Measurement. Joshua Rae Martin

Phylogeny Reconstruction

Lab 6: Energizer Turtles

Chapter 16: Evolution Lizard Evolution Virtual Lab Honors Biology. Name: Block: Introduction

GARNET STATIC SHOCK BARK COLLAR

YOU & YOUR PET PET DOORS. get the. freedom. they need

Building An Ubuntu-Powered Cat Feeder

Hello Scratch! by Gabriel Ford, Sadie Ford, and Melissa Ford. Sample Chapter 3. Copyright 2018 Manning Publications

DIY POST MORTEM TECHNIQUE FOR CATTLEMEN

Animal Behavior. Problem Area: Animal Health and Administering Veterinary Care. Corresponding E-unit(s). Danville, IL: CAERT, Inc.

E. H. Federer and W. T. Federer. Abstract. Some aspects of record keeping and data collection are

Bluefang. All-In-One Smart Phone Controlled Super Collar. Instruction Manual. US and International Patents Pending

Shell (cont d) SSE2034: System Software Experiment 3, Fall 2018, Jinkyu Jeong

Ë1506-E-EAL-NE03 Î NOVEMBER EXAMINATION 2015 ENGLISH FIRST ADDITIONAL LANGUAGE 3/3 GRADE 6 %

Finch Robot: snap levels 1-3

Needs Assessment Study for. New Animal Shelter. For Shelby County, OH. January 26, 2015

American Rescue Dog Association. Standards and Certification Procedures

288 Seymour River Place North Vancouver, BC V7H 1W6

The FCI Initiative for Young Dog Lovers Worldwide. Basic recomendations ORGANIZING YOUTH ACTIVITIES FOR CYNOLOGICAL VENUES.

Wonderful World I I Vocabulary and Structure. h-l-copt-r dra-ing p-n

CANINE REHABILITATION IN THE GENERAL VETERINARY PRACTICE Stacy Reeder, DVM Animal Hospital of Waynesboro

Integrated Math 1 Honors Module 2 Honors Systems of Equations and Inequalities

COMPARING DNA SEQUENCES TO UNDERSTAND EVOLUTIONARY RELATIONSHIPS WITH BLAST

HALE SECURITY PET DOOR CAT GUARDIAN patent pending

Teaching Assessment Lessons

PARADE COLLEGE Mathematics Methods 3&4-CAS Probability Analysis SAC 2

Table 2. Pen floor space for housed unshorn in-lamb ewes Type of ewe Slats m2 Bedded m2. By Edward Egan, Teagasc Drystock Advisor Meath.

House Martin Survey 2016

Fly and Cockroach-2A-2

Solving Problems Part 2 - Addition and Subtraction

Quick Setup Guide Model 5134G

Level 2 Signs with Explanations A4.indd 1 09/04/ :35:50

Lab Assignment #1: Clicker Training.

How to use Mating Module Pedigree Master

INDIAN SCHOOL AL WADI AL KABIR DEPARTMENT OF EVS ( ) REVISION WORKSHEET NAME: CLASS: III SEC: ROLL NO:

Your web browser (Safari 7) is out of date. For more security, comfort and the best experience on this site: Update your browser Ignore

Scratch. To do this, you re going to need to have Scratch!

.1875 NON-LIVE AREA NON-LIVE AREA NON-LIVE AREA. Visit Us At Disney.com/JungleBook Disney

CAT MATH AN INTERMEDIATE LEVEL MATH LESSON ON CAT OVERPOPULATION

Activity 1: Changes in beak size populations in low precipitation

3. $ rosrun turtlesim turtlesim_node (See the turtle with Blue Background leave terminal window running and view turtle)

Our class had 2 incubators full of eggs. On day 21, our chicks began to hatch. In incubator #1, 1/3 of the eggs hatched. There were 2 chicks.

Causes of Aggression

YELLOW VIBRATION BARK COLLAR

Lab Report These are the questions you are asked to answer as you go through the lab. Your lab notebook begins after the questions (page 3).

Rally Obedience Performance Standards. General Procedures

Intro to Animal Assisted Therapy KPETS Keystone Pet Enhanced Therapy Services AAT vs AAA Both AAA and AAT Animals and handlers are screened and

Transcription:

Recursion with Turtles Turtle Graphics Concepts in this slide: A list of all useful functions from the turtle module. Python has a built-in module named turtle. See the Python turtle module API for details. Use from turtle import * to use these commands: CS111 Computer Programming Department of Computer Science Wellesley College fd(dist) bk(dist) rt(angle) pu() pd() pensize(width) pencolor(color) shape(shp) home() clear() reset() setup(width,height) turtle moves forward by dist turtle moves backward by dist turtle turns left angle degrees turtle turns right angle degrees (pen up) turtle raises pen in belly (pen down) turtle lower pen in belly sets the thickness of turtle's pen to width sets the color of turtle's pen to color sets the turtle's shape to shp turtle returns to (0,0) (center of screen) delete turtle drawings; no change to turtle's state delete turtle drawings; reset turtle's state create a turtle window of given width and height Turtle Recursion 19-2 A Simple Example with Turtles Tk window The turtle module has its own graphics environment that is created when we call the function setup. All drawing happens in it. from turtle import * setup(,) fd(100) lt(60) shape('turtle') pencolor('red') fd(150) rt(15) pencolor('blue') bk(100) pu() bk(50) pd() pensize(5) bk(250) pensize(1) home() exitonclick() Concepts in this slide: The only two commands that draw lines are fd and bk. Turtle Recursion 19-3 Looping Turtles (1) Loops can be used in conjunction with turtles to make interesting designs. def polygon(numsides, sidelength): """ Draws a polygon with the specified number of sides, each with the specified length. """ Will solve this in the Notebook. polygon(3,100) polygon(4,100) polygon(6,60) polygon(100,3) polygon(5,75) Concepts in this slide: The power of abstraction: one function that creates a myriad of different shapes. polygon(7,50) Turtle Recursion 19-4

Looping Turtles (2) Spiraling Turtles: A Recursion Example def polyflow(numpetals, petalsides, petallen): """Draws 'flowers' with numpetals arranged around a center point. Each petal is a polygon with petalsides sides of length petallen. """ Will solve this in the Notebook. polyflow(7,4,80) polyflow(10,5,75) polyflow(11,6,60) spiral(200,90,0.9,10) spiral(200,72,0.97,10) spiral(200,80,0.95,10) Answer this: How would you create these shapes using loops? Recursion makes easier solving certain problems that involve a repeating pattern. spiral(200,121,0.95,15) spiral(200,95,0.93,10) Turtle Recursion 19-5 Turtle Recursion 19-6 Spiraling Turtles: A Recursion Example def spiral(sidelen, angle, : """Draw a spiral recursively.""" if sidelen >= minlength: spiral(sidelen*scalefactor, angle, scalefactor, minlength) sidelen is the length of the current side angle is the amount the turtle turns left to draw the next side scalefactor is the multiplicative factor (between 0.0 and 1.0) by which to scale the next side minlength is the smallest side length that the turtle will draw spiral(, 90, spiral(, 90, if sidelen >= minlength: Concepts in this slide: Drawing function call frames helps us follow the execution of recursion. spiral(, 90, Turtle Recursion 19-7 Turtle Recursion 19-8

spiral(, 90, spiral(, 90, spiral(, 90, spiral(, 90, fd() Turtle Recursion 19-9 Turtle Recursion 19-10 spiral(, 90, spiral(, 90, spiral(, 90, fd() spiral(, 90, fd() spiral(, 90, spiral(, 90, if sidelen >= minlength: Turtle Recursion 19-11 Turtle Recursion 19-12

spiral(, 90, spiral(, 90, spiral(, 90, fd() spiral(, 90, spiral(, 90, fd() spiral(, 90, spiral(, 90, spiral(, 90, fd() Turtle Recursion 19-13 Turtle Recursion 19-14 spiral(, 90, spiral(, 90, spiral(, 90, fd() spiral(, 90, spiral(, 90, fd() spiral(, 90, spiral(, 90, fd() spiral(, 90, fd() Turtle Recursion 19-15 if sidelen >= minlength: Turtle Recursion 19-16

spiral(, 90, spiral(, 90, spiral(, 90, fd() spiral(, 90, spiral(, 90, fd() spiral(, 90, spiral(, 90, fd() spiral(, 90, fd() Turtle Recursion 19-17 Turtle Recursion 19-18 spiral(, 90, spiral(, 90, spiral(, 90, fd() spiral(, 90, spiral(, 90, fd() spiral(, 90, spiral(, 90, fd() spiral(, 90, fd() Turtle Recursion 19-19 spiral(, 90, spiral(, 90, if sidelen >= minlength: Turtle Recursion 19-20

spiral(, 90, spiral(, 90, spiral(, 90, fd() spiral(, 90, spiral(, 90, fd() spiral(, 90, spiral(, 90, fd() spiral(, 90, fd() spiral(, 90, spiral(, 90, Turtle Recursion 19-21 spiral(, 90, spiral(, 90, fd() Turtle Recursion 19-22 spiral(, 90, spiral(, 90, spiral(, 90, fd() spiral(, 90, spiral(, 90, fd() spiral(, 90, spiral(, 90, fd() spiral(, 90, fd() spiral(, 90, if sidelen >= minlength: spiral(, 90, spiral(, 90, fd() Turtle Recursion 19-23 spiral(, 90, spiral(, 90, fd() spiral(, 90, Turtle Recursion 19-24

spiral(, 90, spiral(, 90, spiral(, 90, fd() spiral(, 90, spiral(, 90, fd() spiral(, 90, spiral(, 90, fd() spiral(, 90, spiral(, 90, fd() spiral(, 90, fd() spiral(, 90, spiral(, 90, fd() spiral(, 90, Turtle Recursion 19-25 spiral(, 90, spiral(, 90, fd() spiral(, 90, Turtle Recursion 19-26 spiral(, 90, spiral(, 90, spiral(, 90, fd() spiral(, 90, spiral(, 90, fd() spiral(, 90, spiral(204.8, 90, if sidelen >= minlength: spiral(, 90, fd() spiral(, 90, fd() spiral(, 90, fd() spiral(, 90, fd() spiral(204.8, 90, spiral(, 90, spiral(, 90, fd() spiral(, 90, Turtle Recursion 19-27 spiral(, 90, spiral(, 90, fd() spiral(, 90, Turtle Recursion 19-28

spiral(, 90, spiral(, 90, spiral(, 90, fd() spiral(, 90, spiral(204.8, 90, if False: spiral(, 90, fd() spiral(, 90, Important Initially all execution frames co-exist in the memory. Only once a function has returned (implicitly), the execution frame is deleted. spiral(, 90, fd() spiral(, 90, fd() spiral(204.8, 90, spiral(, 90, fd() spiral(, 90, fd() spiral(204.8, 90, spiral(, 90, spiral(, 90, fd() spiral(, 90, Turtle Recursion 19-29 spiral(, 90, spiral(, 90, fd() spiral(, 90, Turtle Recursion 19-31 spiral(, 90, spiral(, 90, spiral(, 90, fd() spiral(, 90, spiral(, 90, fd() spiral(, 90, spiral(, 90, fd() spiral(, 90, fd() spiral(, 90, spiral(, 90, fd() spiral(, 90, Turtle Recursion 19-32 spiral(, 90, Turtle Recursion 19-33

spiral(, 90, spiral(, 90, spiral(, 90, fd() spiral(, 90, spiral(, 90, fd() spiral(, 90, spiral(, 90, fd() Turtle Recursion 19-34 Turtle Recursion 19-35 spiral(, 90, Invariant Spiraling A function is invariant relative to an object s state if the state of the object is the same before and after the function is invoked. Turtle heading x y penisdown 90.0 43.0 79.0 True Important All execution frames were one by one deleted after their completion. This terminates the invocation of the function and has created as a side-effect the turtle image at the top of the slide. def spiralback(sidelen, angle, : """ Draws a spiral. The state of the turtle (position, color, heading, etc.) after drawing the spiral is the same as before drawing the spiral. """ Turtle Recursion 19-36 Turtle Recursion 19-37

How does spiralback work? spiralback(, 90, spiralback(, 90, fd() sidelen angle 90 spiralback(, 90, rt(angle) bk(sidelen) spiralback(, 90, fd() sidelen angle 90 spiralback(, 90, rt(angle) bk(sidelen) spiralback(, 90, sidelen angle 90 spiralback(, 90, rt(angle) bk(sidelen) spiralback(204.8, 90, if False: sidelen 204.8 angle 90 spiralback(sidelen*scalefactor, ) rt(angle) bk(sidelen) spiralback(, 90, fd() sidelen angle 90 spiralback(204.8, 90, rt(angle) bk(sidelen) spiralback(, 90, fd() sidelen angle 90 spiralback(, 90, rt(angle) bk(sidelen) 19-38 Turtle Recursion Be turtle, draw. Essence of Invariance Do state change 1 Do state change 2 Do state change n-1 Do state change n Recursive call to function Undo state change n Undo state change n-1 Undo state change 2 Undo state change 1 Perform changes to state Undo state changes in opposite order Turtle Recursion 19-39 Trees Draw a tree recursively tree(levels, trunklen, angle, shrinkfactor) tree(7, 75, 30, 0.8) tree(7, 75, 15, 0.8) levels is the number of branches on any path from the root to a leaf trunklen is the length of the base trunk of the tree angle is the angle from the trunk for each subtree shrinkfactor is the shrinking factor for each subtree tree(10, 80, 45, 0.7) tree(10, 100, 90, 0.68) Turtle Recursion 19-40 Turtle Recursion 19-41

How to make a 4-level tree: tree(4, 100, 45, 0.6) and two 3-level trees with 60% trunks set at 45 angles Step 2 Make a trunk of size 60 and two 2-level trees with 60% trunks set at 45 angles How to make a 3-level tree: tree(3, 60, 45, 0.6) Make a trunk of size 36 and two 1-level trees with 60% trunks set at 45 angles How to make a 2-level tree: tree(2, 36, 45, 0.6) Step 1 Make a trunk of size 100 Make a trunk of size 21.6 and two 0-level trees set at 45 angles Do nothing! How to make a 1-level tree: tree(1, 21.6, 45, 0.6) How to make a 0-level tree: tree(0, 12.96, 45, 0.6) Turtle Recursion 19-42 Turtle Recursion 19-43 def tree(levels, trunklen, angle, shrinkfactor): """Draw a 2-branch tree recursively. levels: number of branches on any path from the root to a leaf trunklen: length of the base trunk of the tree angle: angle from the trunk for each subtree shrinkfactor: shrinking factor for each subtree """ if levels > 0: # Draw the trunk. fd(trunklen) # Turn and draw the right subtree. rt(angle) tree(levels-1, trunklen*shrinkfactor, angle, shrinkfactor) # Turn and draw the left subtree. lt(angle * 2) tree(levels-1, trunklen*shrinkfactor, angle, shrinkfactor) # Turn back and back up to root without drawing. rt(angle) pu() bk(trunklen) pd() Turtle Recursion 19-48 Tracing the invocation of Turtle Recursion 19-49

Draw trunk and turn to draw level 2 tree Begin recursive invocation to draw level 2 tree Turtle Recursion 19-50 Turtle Recursion 19-51 Draw trunk and turn to draw level 1 tree Begin recursive invocation to draw level 1 tree Turtle Recursion 19-52 Turtle Recursion 19-53

Draw trunk and turn to draw level 0 tree Begin recursive invocation to draw level 0 tree Turtle Recursion 19-54 Turtle Recursion 19-55 Complete level 0 tree and turn to draw another level 0 tree Begin recursive invocation to draw level 0 tree Turtle Recursion 19-56 Turtle Recursion 19-57

Complete level 0 tree and return to starting position of level 1 tree Complete level 1 tree and turn to draw another level 1 tree Turtle Recursion 19-58 Turtle Recursion 19-59 Begin recursive invocation to draw level 1 tree Draw trunk and turn to draw level 0 tree Turtle Recursion 19-60 Turtle Recursion 19-61

Complete two level 0 trees and return to starting position of level 1 tree Complete level 1 tree and return to starting position of level 2 tree Turtle Recursion 19-62 Turtle Recursion 19-63 Complete level 2 tree and turn to draw another level 2 tree Draw trunk and turn to draw level 1 tree Turtle Recursion 19-64 Turtle Recursion 19-65

Draw trunk and turn to draw level 0 tree Complete two level 0 trees and return to starting position of level 1 tree Turtle Recursion 19-66 Turtle Recursion 19-67 Complete level 1 tree and turn to draw another level 1 tree Draw trunk and turn to draw level 0 tree Turtle Recursion 19-68 Turtle Recursion 19-69

Complete two level 0 trees and return to starting position of level 1 tree Turtle Recursion 19-70 Complete level 1 tree and return to starting position of level 2 tree Turtle Recursion 19-71 Complete level 2 tree and return to starting position of level 3 tree bk(60) Turtle Recursion 19-72 Trace the invocation of tree(3, 60, 45, 0.6) 1 bk(60) 2 5 3 6 4 7 Be the turtle, draw the tree, label trunks with i. Turtle Recursion 19-73

The squirrels aren't fooled Random Trees Turtle Recursion 19-74 def treerandom(length, minlength, thickness, minthickness, minangle, maxangle, minshrink, maxshrink): if (length < minlength) or (thickness < minthickness): # Base case pass # Do nothing else: angle1 = random.uniform(minangle, maxangle) angle2 = random.uniform(minangle, maxangle) shrink1 = random.uniform(minshrink, maxshrink) shrink2 = random.uniform(minshrink, maxshrink) pensize(thickness) fd(length) rt(angle1) treerandom(length*shrink1, minlength, thickness*shrink1, minthickness, minangle, maxangle, minshrink, maxshrink) lt(angle1 + angle2) treerandom(length*shrink2, minlength, thickness*shrink2, minthickness, minangle, maxangle, minshrink, maxshrink) rt(angle2) pensize(thickness) bk(length) Turtle Recursion 19-75 More resources Full Slides: http://cs111.wellesley.edu/lectures/lecture19 All steps of recursion examples, drawn out. Exercises for drawing Koch curves and snowflakes with recursive turtle functions. History about turtles at Wellesley and elsewhere. Applying the turtle programming abstraction to control laser cutters in the WeLab (Wellesley engineering lab). Drawing fractals Koch Curve koch(levels, size) koch(0, 150) koch(1, 150) koch(2, 150) koch(3, 150) snowflake(3,150) 4-76 Turtle Recursion 19-77

Turtle Ancestry Snowflakes snowflake(0,150) snowflake(1,150) snowflake(2,150) o Floor turtles used to teach children problem solving in late 1960s. Controlled by LOGO programming language created by Wally Feurzeig (BBN), Daniel Bobrow (BBN), and Seymour Papert (MIT). o Logo-based turtles introduced around 1971 by Papert's MIT Logo Laboratory. o Turtles play a key role in constructionist learning philosophy espoused by Papert in Mindstorms (1980). snowflake(3,150) Turtle Recursion 19-78 Turtle Recursion 19-79 Turtles, Buggles, & Friends At Wellesley Turtle Ancestry (cont d) Richard Pattis s Karel the Robot (1981) teaches problem-solving using Pascal robots that manipulate beepers in a grid world. o Turtle Geometry book by Andrea disessa and Hal Abelson (1986). o LEGO/Logo project at MIT (Mitchel Resnick and Steve Ocko, 1988); evolves into Handyboards (Fred Martin and Brian Silverman), Crickets (Robbie Berg @ Wellesley), and LEGO Mindstorms o StarLogo programming with thousands of turtles in Resnick s Turtles, Termites, and Traffic Jams (1997). o o o o o o o Turtle Recursion 19-80 In mid-1980s, Eric Roberts teaches programming using software-based turtles. In 1996, Robbie Berg and Lyn Turbak start teaching Robotic Design Studio with Sciborgs. In 1996, Randy Shull and Takis Metaxas use turtles to teach problem solving in CS110. In 1997, BuggleWorld introduced by Lyn Turbak when CS111 switches from Pascal to Java. Turtles are also used in the course In 2006, Robbie Berg and others introduce PICO Crickets: http://www.picocricket.com In 2011, Lyn Turbak and the TinkerBlocks group introduce TurtleBlocks, a blocksbased turtle language whose designs can be turned into physical artifacts with laser and vinyl cutters. Turtle Recursion 19-81

Laser Cutting a Tree with Turtle Blocks regular mode boundary mode laser cutting Turtle Recursion 19-82