Exercises
Exercise 2.1. [*]
DrScheme doesn’t provide the function line
for drawing a
Use the language
level BS w/ Lists |
color-list→image
, which consumes a list of colors and produces an
image.
Develop the function make-line
, which creates a ColorList
that
represents a horizontal line. The function consumes the width of the line (a
natural number) and a color. It produces an appropriate list of colors.
Also, even if DrScheme did not provide rectangle
, we could
still draw rectangles. Develop the function make-rectangle
. It
consumes two natural numbers, width
and height
, and a
color. Its result is a ColorList that represents the matching rectangle.
Hint: (append (list 1 2) (list 3 4 5))
produces (list 1 2 3 4 5)
.
After you have developed the functions, including automated tests, run them as follows:
( color-list→ image (make-line 100 (make-color 100 100 100)) 100 1) or ( color-list→ image (make-rectangle 2 3 (make-color 0 0 0)) 2 3)
Keep in mind that these are not tests.
Exercise 2.2. [*] Copy and paste the program from section 2.5. Provide a definition
Add the
teachpack image.ss |
UFO
(in the lecture code) so that it shows up as a flying
green saucer.
Exercise 2.3. [difficult, but entertaining]
Develop place-blocks
. The program consumes a list of blocks:
Add the
teachpack image.ss |
;; A BlockList is either: ;; ---empty
;; ---(cons N BlockList)
Each number on a BlockList
represents the height of a stack of
blocks. The program produces an image, drawing a stack of blocks of the
specified height starting in the lower, right corner of a 100 x 100
canvas.
Note: The program is a core piece of a Tetris-style game.
Hint 1: Use these definitions for your basic constants:
(define WIDTH 100) (define HEIGHT 100) (define CANVAS (empty-scene WIDTH HEIGHT)) (define BLOCK-WIDTH 10) (define BLOCK (offset-image+ (rectangle BLOCK-WIDTH BLOCK-WIDTH 'red) 0 0 (outline-rect BLOCK-WIDTH BLOCK-WIDTH 'black)))
Hint 2: Here is the image for (place-blocks (list 1 2))
:
image=?
to test the program. The picture shows that the drawing starts at the
right. The length
of the list determines how far from the right
a stack of blocks is drawn.
Exercise 2.4. [*] Use the conventional draw.ss teachpack to draw and erase green flying saucers from a canvas.
Add the
teachpack draw.ss |
draw-ufo
and clear-ufo
for drawing and clearing a
UFO-like shape from a canvas. Both functions consume a Posn
, which
represents the anchor position of the UFO (e.g., the center of the disk or
the northwest corner of the rectangle).