Comp 200 Lecture 10
Drawing Pentagons and Fractals (in lab)

Wed 06 Feb 2002

Today we'll be in lab, drawing pentagons and Koch curves! Start up DrScheme, start up Netscape, and work with your partner!
Visit the Turtle Graphics info page for an explanation of how to initialize your DrScheme environment to use Turtle functions.

Drawing a Pentagon

New functions: In a pentagon, each angle requires a 72-degree turn. We are going to write a function which takes in a snapshot, and returns a new snapshot with a pentagon added to to the snapshot. (The snapshot we give to the function might be a blank picture, or it might be a picture of my house, or a picture with a bunch of other already-drawn pentagons.) But we'll build up to that step-by-step.
  1. We could draw a pentagon with a whole bunch of nested (turn 72 (draw 50 ...))s. What a drag! To help alleviate this a little, write a function one-pentagon-side: it takes in a snapshot, and returns a snapshot with a pentagon added in (each side 50 long).
    ;; one-pentagon-side: number snapshot --> snapshot
    ;; Return an after-snapshot which is like "env",
    ;; except that it has a one side of a pentagon drawn, of size length,
    ;; along with the 72-degree turn.
    ;;
    (define (one-pentagon-side length env)
       ...)
    
    
    
    
    ; Test cases:
    (one-pentagon-side 40 (turtles 300 100))
    (one-pentagon-side 100 (turtles 300 100))
    
    
  2. Still a drag: To draw an entire pentagon, we'd have to call one-pentagon-side five times. Couldn't the computer count for us? Hmmm, counting... sounds like a job for WholeNums! Write a function many-pentagon-sides which takes in three inputs: a number n, a length length, and a snapshot, and returns a snapshot with n pentagon-sides added in. This function is going to look similar to factorial, or countdown, etc.
    (Follow our ol' recipe, step-by-step.
    ;; many-pentagon-sides: WholeNum length snapshot --> snapshot
    ;;
    (define (many-pentagon-sides n length env)
      (if (zero? n)
          ...
          ...(many-pentagon-sides (sub1 n) length  env)... ))
    
    Note how we make the recursive call on (sub1 n), although we have those n-1 sides drawn directly onto env. What does that call to many-pentagon-sides return?
  3. Now write the function draw-pentagon. It takes as input one number (the length of each side), and a before-snapshot. (Hmmm, what previously-written function will you call?)
  4. Wow, if you managed to finish all these functions, great! Let's go one step further, and write a function which draws a bunch of concentric pentagons.
    Hint: first adapt your draw-pentagon function to take a before-snapshot, and a size of the pentagon.
    Once that's done, you can call your draw-pentagon with smaller and smaller sizes, to draw many (non-concentric) pentagons. Finally, can you figure out how to move, to make the pentagons concentric? (There are several different approaches here. One is to always have draw-pentagon keep the turtle starting and ending in the center of the just-drawn pentagon.)

Factoid: the Pentagon's annual budget is a third of a trillion dollars. (Source: n.y. times chart: "costs")


The Koch Curve: A Simple Fractal

In class I'll talk about a Koch curve, (which looks like a Mt Kiliminjaro). More precisely, that is a level one Koch curve. A level two Koch curve is like level one, except that each straight-line segment is replaced with a copy of the original level-one Koch curve (of appropriate size).

To draw a level-three Koch curve, draw a small level-two curve, turn, draw another small level-two curve, turn again, draw a third level-two curve, turn, and finally draw the last level-two curve.

And it goes on: To draw a level-17 curve, draw a level-16 curve, turn, draw another level-16 curve,

A level three Koch curve is like level two, except that each of the level two's segments is replaced with a smaller copy of the (level-one) Koch curve. An alternate way of looking at it is that a level-n Koch curve is like a level-one curve, except that each segement is replaced with a smaller level-(n-1).

Fill in the blanks below, and try it out!

;; koch: natNum number snapshot --> snapshot
;; draws a koch curve of level levels deep, a distance of length, based 
;; on the snapshot env. It returns a new snapshot.
(define (koch level length env) 
    (if (zero? level)
        ...                                                  ; What is a zero-level koch curve?
        ... ... ... (koch (sub1 level) ... env))... ... ...) ; What does (koch (sub1 level) ... env) give us?
Hint: How do you make a koch curve level-1 deep and only a third of length long? How do you make four smaller ones with turns between them?

Now, can you make your code easier for the user to call? That is, a function (snowflake level size), so the user doesn't have to provide an before snapshot?

If you like, you can play with this. Draw three koch curves in a row, but with a -120 turn between them. (Hence the name "snowflake".)

The Koch curve is just one particular fractal curve. Try making up your own base designs, and see what you get! It is not always as easy as it might seem! (Hint: make sure the turtle starts and ends pointing in the same direction. Or, take your Koch curve and see what you get if the turtle doesn't end up quite facing the same way as started.) Maybe you'll want to go to this Fractals Site to get some ideas!


(When finished, we can compare solutions)