Teachpacks for How to Design Programs

# Simple Drawing Exercises

### draw.ss

The teachpack provides two kinds of functions. The first four allow students to simulate a small world of animated drawings and games:
- `big-bang` : Number World -> true;
  `(define (big-bang n w) ...)` start the clock, one tick every `n` seconds; `w` becomes the first world
- `on-key-event` : ((union char symbol) World -> World) -> true;
  add a function to the world that processes keyboard events
- `on-tick-event` : (World -> World) -> true;
  add a function to the world that processes tick events
- `end-of-time` : -> World;
  stop the world, return the last world
The world consists of a canvas and whatever the tick and keyevent handlers draw on it. For the use of these functions, see the HtDP+ material. With the reminder, the students can write functions that draw into this world:
- `start` : Number Number -> true;
  opens a canvas of specified size
- `start/cartesian-plane` : Number Number -> true;
  opens a canvas of specified size and draws a Cartesian plane
- `stop` : -> true (no arguments);
  closes the canvas
- `draw-circle` : Posn Number Symbol -> true;
  draws a circle at posn with given radius and color
- `draw-solid-disk` : Posn Number Symbol -> true;
  draws a disk at posn with given radius and color
- `draw-solid-rect` : Posn Number Number Symbol -> true;
  draws a rectangle at posn with given width, height, and color
- `draw-solid-line` : Posn Posn Symbol -> true;
  draws a line from one posn to other
- `draw-solid-string` : Posn String -> true;
  draws a string at posn
- `wait-for-mouse-click` : -> Posn;
  waits for the user to click on the mouse, within the window (the operation is a quasi-constructor for posns)
- `get-key-event` : -> false or Character or Symbol ;
  checks whether the user has pressed a key within the window; its result is
  1. `false`, if the user didn't press a key;
  2. `Character`, if the user pressed an alphanumeric key;
  3. `Symbol`, if the user pressed, for example, an arror key: `'up` `'down` `'left` `'right`
- `sleep-for-a-while` : Number -> true;
  suspends evaluation for the given number of seconds
- The following symbols are recognized as colors:

  > `'white` `'yellow` `'red` `'blue` `'green` `'black`

  For other colors, guess! For example, `'orange` works, but `'mauve` doesn't. If you apply the function to a symbol that it doesn't recognize as a color, it raises an error.
The teachpack also provides `clear-` operations for each `draw-` operation. The arguments are the same. Note: use `clear-rectangle` instead of `clear-string` for now.
The color argument for all functions are optional.

Sample session: Set teachpack to `draw.ss` and execute:
`> (start 500 500)`
`> (draw-solid-disk (make-posn 100 100) 3 'red)`
`true`
`> (clear-solid-disk (make-posn 100 100) 3 'red)`
`true`
`> (sleep-for-a-while 1)`
`> (draw-solid-disk (make-posn 100 100) 3 'red)`
`true`
`> (clear-solid-disk (make-posn 100 100) 3)`
`true`
`> (stop)`
`>`
This session opens a window, draws a red disk, clears it, sleeps for a second, and then repeats. The last expression closes the canvas. See `http://www.ccs.neu.edu/home/matthias/HtDP/Extended/` for an example on how to use `get-key-event`. The program is the basis for an extended exercise under development.