Comp 200 Lecture
Drawing a pentagon (in lab)

Today we'll be in lab, drawing polygons. Start up drscheme, start up netscape, visit this page (http://www.owlnet.rice.edu/~comp200/symonds.html), and work with your partner!
Re-cap from monday:

Turtle graphics

DrScheme also has turtle graphics. To enable this, you must (one time only) select the turtles teachpack:
  1. From the Language menu, select Add Teachpack....
  2. You should see a directory with only a couple items, one of which is turtles.ss; select it.
    (If you don't see that, you might have to navigate to the right directory: /home/scheme/plt/teachpack/turtle.ss)
  3. After the next time you press Execute, you'll have access to the turtle functions mentioned below.

  1. (turtles 300 100) returns a snapshot of a 200-by-200 sandbox with a droopy-tailed turtle in the middle, facing east.
  2. We then have functions which take in snapshots and return new snapshots: (turn 90 (turtle 300 100)) returns a snapshot of a turtle facing north.
  3. We can then take this new snapshot, and get a snapshot with a moved turtle: (draw 50 (turn 90 (turtle 300 100))).
If you want, you can also use define to give names to intermediate snapshots.

In summary, the four new functions are:

Example Thus the following is a snapshot of two sides of a triangle, given the name 2sides:

(define 2sides (turn 120 (draw 50 (turn 120 (draw 50 (turtles 300 100))))))
(define tiny-triangle (turn -45 (move -20 (turn 120 (draw 10 (turn 120 (draw 10 (turn 120 (draw 10 (turtles 300 100))))))))))
(You can evaluate the expression 2sides, and you'll see what the snapshot that this placeholder evaluates to.)
Once this is done, we can take in that snapshot and draw the last side:
(turn 120 (draw 50 2sides))
This returns a snapshot with three sides.
(Note that 2sides is still the same snapshot it always was; move doesn't change the snapshot it's given any more than + changes the numbers it's given.)
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 (move 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 n before): number, snapshot --> snapshot
    ;; Return an after-snapshot which is like "before",
    ;; except that it has a one side of a pentagon drawn (length n),
    ;; along with the 72-degree turn.
    ;;
    (define (one-pentagon-side n before)
       ...)
    
    
    
    
    ; Test cases:
    (one-pentagon-side 40 (turtles 300 100))
    
    ; The snapshot of a triangle mentioned  above:
    (define tiny-triangle (turn -45 (move -20 (turn 120 (draw 10 (turn 120 (draw 10 (turn 120 (draw 10 (turtles 300 100))))))))))
    
    ; Let's make a snapshot of a pentagon around a triangle:
    ;
    (one-pentagon-side 40 tiny-triangle)
    
  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 NatNums! Write a function many-pentagon-sides which takes in two inputs: a number n 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. Here's a hint, half-completed:)
    ;; (many-pentagon-sides n before-snapshot): NatNum, snapshot --> snapshot
    ;;
    (define (many-pentagon-sides n before-snapshot)
      (if (zero? n)
          ...
          ...(many-pentagon-sides (sub1 n) before-snapshot)... ))
    
    Note how we make the recursive call on (sub1 n), although we have those n-1 sides drawn directly onto before-snapshot. What does that call to many-pentagon-sides return?
  3. Now write the function 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. Write a function which draws a hexagon. A dodecagon (12)? Hey, let's not write a different function for each type of polygon; let's write one function that does them all! What does this function take as input?
  5. 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 budet is a third of a trillion dollars. (Source: n.y. times chart: "costs")

NatNum ;; The number of times you can double-or-nothing a $1 bet, ;; if you start with $n. ;; ;; This is an alternate implementation, that might be more intuitive ;; for some people: keep track of the current-bet. ;; (define (nod2 n) (nod2-help 1 n)) ;; (nod2-help curr-bet n): number, number --> NatNum ;; Given that the current bet is $curr-bet, ;; how many further times can it be double-or-nothing'd, ;; if we have $n? ;; (define (nod2-help curr-bet n) (if (> curr-bet n) ... ... (nod2-help ...curr-bet... n))) (Fill in the above ...; you can compare your soln.) -->