Comp 200 Turtle Graphics


Turtle Graphics

DrScheme has Turtle Graphics, aka Logo, the "old-school" name. (Myrtle lives! ) 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 300-by-100 sandbox with a droopy-tailed turtle in the middle, facing east.
  2. We then have functions which take in snapshots (environments) 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.)

Here's an example function that will draw a triangle, given a snapshot:
;;draw-triangle: snapshot -> snapshot
;;draws a triangle within the given snapshot
(define (draw-triangle env)
  (draw-triangle-help 3 env))
 

;;draw-triangle-help: number snapshot -> snapshot
;;recursive helper for draw-triangle
(define (draw-triangle-help n env)
  (if (= n 0)
      env
      (draw-triangle-help (sub1 n) (turn 120 (draw 50 env)))))
Just call (draw-triangle (turtles 400 300)) to invoke it.