Google

link="#009900" vlink="#007700" alink="#cc0000"> Teachpacks for How to Design Programs

Simple Drawing Exercises


draw.ss

The teachpack draw.ss provides a miniscule collection of graphics functions:
  • 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
  • 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
    Applying the functions to any other symbol triggers an error.



  • The teachpack also provides clear- operations for each draw- operation. The arguments are the same.


    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.