Turtle Graphics Recursin with Turtles Pythn has a built-in mdule named turtle. See the Pythn turtle mdule API fr details. Use frm turtle imprt * t use these cmmands: CS111 Cmputer Prgramming Department f Cmputer Science Wellesley Cllege fd(dist) bk(dist) rt(angle) pu() pd() pensize(width) penclr(clr) shape(shp) hme() clear() reset() setup(width,height) turtle mves frward by dist turtle mves backward by dist turtle turns left angle degrees turtle turns right angle degrees (pen up) turtle raises pen in belly (pen dwn) turtle lwer pen in belly sets the thickness f turtle's pen t width sets the clr f turtle's pen t clr sets the turtle's shape t shp turtle returns t (0,0) (center f screen) delete turtle drawings; n change t turtle's state delete turtle drawings; reset turtle's state create a turtle windw f given width and height 10-2 A Simple Example with Turtles frm turtle imprt * setup(,) fd(100) lt(60) shape('turtle') penclr('red') fd(150) rt(15) penclr('blue') bk(100) pu() bk(50) pd() pensize(5) bk(250) pensize(1) hme() 10-3 Lping Turtles Lps can be used in cnjunctin with turtles t make interesting designs. plygn(3,100) # Draws a plygn with the specified number # f sides, each with the specified length def plygn(numsides, sidelength): plygn(4,100) plygn(6,60) plygn(100,3) plygn(5,75) plygn(7,50) 10-4
Lping Turtles Spiraling Turtles: A Recursin Example # Draws "flwers" with numpetals arranged arund # a center pint. Each petal is a plygn with # petalsides sides f length petallen. def plyflw(numpetals, petalsides, petallen): plyflw(7,4,80) plyflw(10,5,75) plyflw(11,6,60) spiral(200,90,0.9,10) spiral(200,72,0.97,10) spiral(200,80,0.95,10) 10-5 spiral(200,121,0.95,15) spiral(200,95,0.93,10) 10-6 spiral(sidelen, angle, sidelen is the length f the current side angle is the amunt the turtle turns left t draw the next side scalefactr is the multiplicative factr by which t scale the next side (it is between 0.0 and 1.0) minlength is the smallest side length that the turtle will draw Spiraling Turtles: A Recursin Example def spiral(sidelen, angle, : spiral(, 90, 10-7 10-8
spiral(, 90, spiral(, 90, spiral(, 90, if sidelen >= minlength: 10-9 10-10 spiral(, 90, spiral(, 90, spiral(, 90, spiral(, 90, fd() 10-11 10-12
spiral(, 90, spiral(, 90, spiral(, 90, fd() spiral(, 90, fd() if sidelen >= minlength: 10-13 10-14 spiral(, 90, spiral(, 90, spiral(, 90, fd() spiral(, 90, fd() fd() 10-15 10-16
spiral(, 90, spiral(, 90, spiral(, 90, fd() spiral(, 90, fd() fd() fd() 10-17 if sidelen >= minlength: 10-18 spiral(, 90, spiral(, 90, spiral(, 90, fd() spiral(, 90, fd() fd() fd() 10-19 10-20
spiral(, 90, spiral(, 90, spiral(, 90, fd() spiral(, 90, fd() fd() fd() 10-21 spiral(, 90, spiral(, 90, if sidelen >= minlength: 10-22 spiral(, 90, spiral(, 90, spiral(, 90, fd() spiral(, 90, fd() fd() fd() spiral(, 90, spiral(, 90, 10-23 spiral(, 90, spiral(, 90, fd() 10-24
spiral(, 90, spiral(, 90, spiral(, 90, fd() spiral(, 90, fd() fd() fd() spiral(, 90, if sidelen >= minlength: spiral(, 90, spiral(, 90, fd() 10-25 spiral(, 90, spiral(, 90, fd() spiral(, 90, 10-26 spiral(, 90, spiral(, 90, spiral(, 90, fd() spiral(, 90, fd() fd() spiral(, 90, fd() spiral(, 90, fd() spiral(, 90, spiral(, 90, fd() spiral(, 90, 10-27 spiral(, 90, spiral(, 90, fd() spiral(, 90, 10-28
spiral(, 90, spiral(, 90, spiral(, 90, fd() spiral(, 90, fd() spiral(204.8, 90, if sidelen >= minlength: fd() spiral(, 90, fd() fd() spiral(, 90, fd() spiral(204.8, 90, spiral(, 90, spiral(, 90, fd() spiral(, 90, 10-29 spiral(, 90, spiral(, 90, fd() spiral(, 90, 10-30 spiral(, 90, spiral(, 90, spiral(, 90, fd() spiral(204.8, 90, if False: spiral(, 90, fd() fd() spiral(, 90, fd() spiral(204.8, 90, fd() spiral(, 90, fd() spiral(204.8, 90, spiral(, 90, spiral(, 90, fd() spiral(, 90, 10-31 spiral(, 90, spiral(, 90, fd() spiral(, 90, 10-32
spiral(, 90, spiral(, 90, spiral(, 90, fd() spiral(, 90, fd() fd() fd() spiral(, 90, spiral(, 90, fd() spiral(, 90, 10-33 spiral(, 90, 10-34 spiral(, 90, spiral(, 90, spiral(, 90, fd() spiral(, 90, fd() fd() 10-35 10-36
spiral(, 90, Invariant Spiraling A functin is invariant relative t an bject s state if the state f the bject is the same befre and after the functin is invked. # Draws a spiral. The state f the turtle (psitin, # clr, heading, etc.) after drawing the spiral is the # same as befre drawing the spiral. def spiralback(sidelen, angle, : 10-37 10-38 Zigzags zigzag(1, 10) Trees zigzag(4, 10) # Draws the specified number f zigzags with the specified # length. def zigzag(num, length): if num>0: lt(45) fd(length) rt(90) fd(2*length) fd(length) zigzag(num-1, length) tree(7, 75, 30, 0.8) tree(7, 75, 15, 0.8) Exercise: mdify zigzag t make the turtle s state invariant. 10-39 tree(10, 80, 45, 0.7) tree(10, 100, 90, 0.68) 10-40
Hw t make a 4 level tree: tree(4, 100, 45, 0.6) Hw t make a 3 level tree: tree(3, 60, 45, 0.6) and tw level 3 trees with 60% trunks set at 45 angles and tw level 2 trees with 60% trunks set at 45 angles Make a trunk f size 60 Make a trunk f size 100 10-41 10-42 Hw t make a 2 level tree: tree(2, 36, 45, 0.6) Hw t make a 1 level tree: tree(1, 21.6, 45, 0.6) and tw level 1 trees With 60% trunks set at 45 angles and tw level 0 trees set at 45 angles Make a trunk f size 36 Make a trunk f size 21.6 10-43 10-44
Hw t make a 1 level tree: tree(0, 12.96, 45, 0.6) tree(levels, trunklen, angle, shrinkfactr) D nthing! levels is the number f branches n any path frm the rt t a leaf trunklen is the length f the base trunk f the tree angle is the angle frm the trunk fr each subtree shrinkfactr is the shrinking factr fr each subtree 10-45 10-46 Trees Tracing the invcatin f tree(3, 60, 45, 0.6) def tree(levels, trunklen, angle, shrinkfactr): 10-47 10-48
Draw trunk and turn t draw level 2 tree Begin recursive invcatin t draw level 2 tree 10-49 10-50 Draw trunk and turn t draw level 1 tree Begin recursive invcatin t draw level 1 tree 10-51 10-52
Draw trunk and turn t draw level 0 tree Begin recursive invcatin t draw level 0 tree 10-53 10-54 Cmplete level 0 tree and turn t draw anther level 0 tree Begin recursive invcatin t draw level 0 tree 10-55 10-56
Cmplete level 0 tree and return t starting psitin f level 1 tree Cmplete level 1 tree and turn t draw anther level 1 tree 10-57 10-58 Begin recursive invcatin t draw level 1 tree Draw trunk and turn t draw level 0 tree 10-59 10-60
Cmplete tw level 0 trees and return t starting psitin f level 1 tree Cmplete level 1 tree and return t starting psitin f level 2 tree 10-61 10-62 Cmplete level 2 tree and turn t draw anther level 2 tree Draw trunk and turn t draw level 1 tree 10-63 10-64
Draw trunk and turn t draw level 0 tree Cmplete tw level 0 trees and return t starting psitin f level 1 tree 10-65 10-66 Cmplete level 1 tree and turn t draw anther level 1 tree Draw trunk and turn t draw level 0 tree 10-67 10-68
Cmplete tw level 0 trees and return t starting psitin f level 1 tree 10-69 Cmplete level 1 tree and return t starting psitin f level 2 tree 10-70 Cmplete level 2 tree and return t starting psitin f level 3 tree bk(60) 10-71 The squirrels aren't fled 10-72
Randm Trees Turtle Ancestry def treerandm(length, minlength, thickness, minthickness, minangle, maxangle, minshrink, maxshrink): if (length < minlength) r (thickness < minthickness): # Base case pass # D nthing else: angle1 = randm.unifrm(minangle, maxangle) angle2 = randm.unifrm(minangle, maxangle) shrink1 = randm.unifrm(minshrink, maxshrink) shrink2 = randm.unifrm(minshrink, maxshrink) pensize(thickness) fd(length) rt(angle1) treerandm(length*shrink1, minlength, thickness*shrink1, minthickness, minangle, maxangle, minshrink, maxshrink) lt(angle1 + angle2) treerandm(length*shrink2, minlength, thickness*shrink2, minthickness, minangle, maxangle, minshrink, maxshrink) rt(angle2) pensize(thickness) bk(length) 10-73 Flr turtles used t teach children prblem slving in late 1960s. Cntrlled by LOGO prgramming language created by Wally Feurzeig (BBN), Daniel Bbrw (BBN), and Seymur Papert (MIT). Lg-based turtles intrduced arund 1971 by Papert s MIT Lg Labratry. Turtles play a key rle in cnstructinist learning philsphy espused by Papert in Mindstrms (1980). 10-74 Turtle Ancestry (cnt d) Turtles, Buggles, & Friends At Wellesley Richard Pattis s Karel the Rbt (1981) teaches prblem-slving using Pascal rbts that manipulate beepers in a grid wrld. Turtle Gemetry bk by Andrea disessa and Hal Abelsn (1986). LEGO/Lg prject at MIT (Mitchel Resnick and Steve Ock, 1988); evlves int Handybards (Fred Martin and Brian Silverman), Crickets (Rbbie Berg @ Wellesley), and LEGO Mindstrms StarLg prgramming with thusands f turtles in Resnick s Turtles, Termites, and Traffic Jams (1997). 10-75 In mid-1980s, Eric Rberts teaches prgramming using sftware-based turtles. In 1996, Rbbie Berg and Lyn Turbak start teaching Rbtic Design Studi with Scibrgs. In 1996, Randy Shull and Takis Metaxas use turtles t teach prblem slving in CS110. In 1997, BuggleWrld intrduced by Lyn Turbak when CS111 switches frm Pascal t Java. Turtles are als used in the curse In 2006, Rbbie Berg and thers intrduce PICO Crickets: http://www.piccricket.cm In 2011, Lyn Turbak and the TinkerBlcks grup intrduce TurtleBlcks, a blcks-based turtle language whse designs can be turned int physical artifacts with laser and vinyl cutters. 10-76
Laser Cutting a Tree regular mde bundary mde laser cutting TurtleWrld 10-77