[previous] [up] [next]     [index]
Next: Natural Numbers Up: More on Processing Lists Previous: Lists that Contain Structures

Extended Exercise: Moving Pictures

In sections [cross-reference] and [cross-reference], we studied how to move individual shapes. A picture, however, isn't just a single shape but a whole collection of them. Considering that we have to draw, translate, and clear pictures, and that we may wish to change a picture or manage several pictures at the same time, it is best to collect all of the parts of a picture into a single piece of data. Because pictures may consist of a varying number of items, a list representation for pictures naturally suggests itself.


Exercises

Exercise 10.3.1

Provide a data definition that describes the class of lists of shapes. The class of shapes was defined in exercise [cross-reference].

Create a sample list that represents the face of figure [cross-reference] and name it FACE. Its basic dimensions are gathered in the following table:

tabular11659

The table assumes a canvas of size 300 by 100.

Develop the template fun-for-losh, which outlines functions that consume a list-of-shapesSolution

Exercise 10.3.2

Use the template fun-for-losh to develop the function draw-losh. It consumes a list-of-shapes, draws each item on the list, and returns true. Remember to use (start n m) to create the canvas before the function is used. Solution

Exercise 10.3.3

Use the template fun-for-losh to develop translate-losh. The function consumes a list-of-shapes and a number delta. The result is a list of shapes where each of them has been moved by delta pixels in the x direction. The function has no effect on the canvas. Solution

Exercise 10.3.4

Use the template fun-for-losh to develop clear-losh. The function consumes a list-of-shapes, erases each item on the list from the canvas, and returns trueSolution

Exercise 10.3.5

Develop the function draw-and-clear-picture. It consumes a picture. Its effect is to draw the picture, sleep for a while, and to clear the picture. Solution

Exercise 10.3.6

Develop the function move-picture. It consumes a number (delta) and a picture. It draws the picture, sleeps for a while, clears the picture and then produces a translated version. The result should be moved by delta pixels.

Test the function with expressions like these:

(start 500 100)

(draw-losh (move-picture -5 (move-picture 23 (move-picture 10 FACE))))

(stop)

This moves FACE (see exercise [cross-reference]) by 10, 23, and -5 pixels in the x direction. Solution

When the function is fully tested, use the teachpack arrow.ss and evaluate the expression:

(start 500 100)

(control-left-right FACE 100 move-picture draw-losh)

The last one creates a graphical user interface that permits users to move the shape FACE by clicking on arrows. The shape then moves in increments of 100 (right) and -100 (left) pixels. The teachpack also provides arrow controls for other directions. Use them to develop other moving pictures.


[previous] [up] [next]     [index]
Next: Natural Numbers Up: More on Processing Lists Previous: Lists that Contain Structures

PLT