Skip to main content

Assignment 7: Turtles and iteration

Due: Tuesday, April 11 by 10:30pm

Summary: In this assignment, you will use turtles to create images using iteration, recursion, and a mix of both techniques.

Purposes: To give you more experience with the turtle model. To give you more comfort with anonymous procedures. To explore the complexities possible from simple operations.

Collaboration: You may work alone or in groups of two or three students. You may choose your own partners.

Submitting: Send your answer to csc151-01-grader@grinnell.edu. The title of your email should have the form [CSC 151.01] Assignment 7 and should contain your answers to all parts of the assignment. I prefer that you put your answers in the body of the message, rather than as an attachment.

So that this assignment is a learning experience for everyone, we may spend class time publicly critiquing your work.

Problem 1: Drawing Polygons

Topics: turtle graphics, iteration

Document and write a procedure, (turtle-polygon! turtle side-length sides), that uses a turtle to draw a regular polygon with the specified number of sides, with each side of the specified length.

Important: Your procedure must return the turtle to its original position and angle. Your procedure must not change the turtle’s brush or color.

Hint: Use repeat. The turtle must turn a total of 360 degrees to return to its original angle.

For example,

A triangle with sides of length 100 (turtle-polygon! t 100 3)

A square with sides of length 100 (turtle-polygon! t 100 4)

A pentagon with sides of length 60 (turtle-polygon! t 60 5)

A hexagon with sides of length 40 (turtle-polygon! t 40 6)

Problem 2: Drawing Snowflakes

Topics: turtle graphics, iteration, recursion

Document and write a procedure, (turtle-snowflake! turtle side-length sides depth), that recursively draws a polygon with smaller polyings on the outside of each corner on the first polygin, even smaller polygons at the corners of the smaller polygons, and so on for depth levels. Each successive level should have a side length that is 40% that of the previous level.

For example: the following call draws an inner square with edges of length 100, then at each corner places an additional square with edges of length 40.

A square with edges of length 100.  At each corner, there is a smaller square with edges of length 40 (turtle-snowflake! t 100 4 2)

If we change the last parameter to 4, our procedure draws the same two-level image as the previous example, but adds two more levels. That is, each 40x40 square has a 16x16 (40% x 40 = 16) square at each corner, and each 16x16 square has a 6.4x6.4 square at each corner.

A 100-by-100 square.  At each corner, there is a 40-by-40 square.  At each corner of the 40-by-40 squares, there is a 16-by-16 square.  At each corner of the 16-by-16 squares, there is a 6.4-by-6.4 square. (turtle-snowflake! t 100 4 4)

If we use 5 for the number of sides and 2 for the number of levels, we get a pentagon with edges that are 100 pixels long and a smaller pentagon at each corner.

A pentagon with edges of length 100.  At each vertex, there is a smaller pentagon whose edges are of length 40 (turtle-snowflake! t 100 5 2)

If we increase the level to 4, we may see why the procedure is called turtle-snowflake!.

A pentagon with edges of length 100.  At each vertex, there is a smaller pentagon whose edges are of length 40.  At each vertex, there is a smaller pentagon whose edges are of length 16.  And at each vertex, there is an even smaller pentaon whose edges are of length 6.4 (turtle-snowflake! t 100 5 4)

The edge lengths in this example are 100, 40, 16, and 6.4.

Of course, not every result will be as nice. If we increase the number of sides to 6, we may see some unexpected overlap.

A hexagon with edges of length 100.  At each vertex, there is a smaller hexagon whose edges are of length 40.  At each vertex, there is a smaller hexagon whose edges are of length 16.  And at each vertex, there is an even smaller pentaon whose edges are of length 6.4.  The small hexagons often overlap with each other. (turtle-snowflake! t 100 6 4)

While you can certainly have more than six edges or more than four levels, the drawing will take quite a while to finish.

Note: Your turtle-polygon! procedure may be a useful starting point, but you probably won’t be able to use it directly in your implementation of turtle-snowflake!. You may find it useful to mix iteration and recursion: use iteration to draw each side of a polygon, and recursion to draw each level.

Problem 3: Turtle Trees

Topics: basic recursion, numeric recursion, turtle graphics, Documentation

Write and document a procedure, (turtle-tree! turtle trunk-length branching-factors), that takes three parameters: a turtle, a positive integer, and a list of integers between 2 and

  1. Your procedure should use the provided turtle to draw a tree. You can think of a tree as a trunk (a line) with some number of smaller trees “growing” out of the end of the trunk.

To draw a tree with a particular trunk length, your turtle should first move forward by the trunk length. You should then take the first element of branching-factors and recursively draw that many slightly smaller trees at that point, spaced evenly from -45 degrees to 45 degrees. A factor of 75% for the new branch length tends to work well.

Here are some simple examples.

A straight line going upward from the bottom of the image. (turtle-tree! (set-up-turtle) 80 null)

A straight line going upward from the bottom of the image.  Two lines branch out, one at -45 degrees and one at 45 degrees. (turtle-tree! (set-up-turtle) 80 (list 2))

A straight line going upward from the bottom of the image.  Three lines branch out, one at -45 degrees, one at 0 degrees, and one at 45 degrees. (turtle-tree! (set-up-turtle) 80 (list 3))

A straight line going upward from the bottom of the image.  Two lines branch out, one at -45 degrees and one at 45 degrees.  Two lines branch out from each of those two lines (turtle-tree! (set-up-turtle) 80 (list 2 2))

A straight line going upward from the bottom of the image.  Three lines branch out, one at -45 degrees, one at 0 degrees, and one at 45 degrees.  Three lines then branch out from each of those three lines (turtle-tree! (set-up-turtle) 80 (list 3 3))

A straight line going upward from the bottom of the image.  Two lines branch out, one at -45 degrees and one at 45 degrees.  Four lines then branch out from each of those three lines (turtle-tree! (set-up-turtle) 80 (list 2 4))

A straight line going upward from the bottom of the image.  Four lines branch out. Two lines then branch out from each of those three lines (turtle-tree! (set-up-turtle) 80 (list 4 2))

As these examples suggest, drawing a tree, some set-up is required:

; Create a world, a turtle, set the brush to a fine line, and position the turtle
; at the bottom middle of the image facing up.
(define set-up-turtle
  (lambda ()
    (let* ([world (image-show (image-new 300 200))]
           [tommy (turtle-new world)])
      (turtle-turn! tommy -90)
      (turtle-teleport! tommy 150 200)
      (turtle-set-brush! tommy "2. Hardness 100" 0.15)
      tommy)))

After running the set-up code above, you should be able to run the prior examples with a completed turtle-tree! function. You should, of course, try other examples, too.