In section
, we developed functions for drawing,
translating, and clearing circles and rectangles. As we have just seen, we
should think of the two classes of data as subclasses of a class of shapes
so that we can just draw, translate, and clear shapes.
Exercise 7.4.1
Provide a data definition for a general class of shapes.
The class
should at least subsume the classes of colored circles and rectangles from
section
.
Develop the template fun-for-shape, which outlines functions that consume shapes. Solution
Exercise 7.4.2
Use the template fun-for-shape to develop draw-shape. The function consumes a shape and draws it on the canvas. Solution
Exercise 7.4.3
Use the template fun-for-shape to develop translate-shape. The function consumes a shape and a number delta, and produces a shape whose key position is moved by delta pixels in the x direction. Solution
Exercise 7.4.4
Use the template fun-for-shape to develop clear-shape. The function consumes a shape, erases it from the canvas, and returns true. Solution
Exercise 7.4.5
Develop the function draw-and-clear-shape. The function consumes a shape, draws it, sleeps for a while, and clears it. If all the effects work out, it produces true. Solution
Exercise 7.4.6
Develop move-shape, which moves a shape across the canvas. The
function consumes a number (delta) and a shape. The function should
draw-and-clear the shape and return a new shape that has been translated by
delta pixels. Use this function several times to move a shape across the
canvas. Solution