[previous] [up] [next]     [index]
Next: Note: Designing Abstractions from Up: Designing Abstractions from Examples Previous: Abstraction and a Single

Extended Exercise: Moving Pictures, Again

In sections [cross-reference], [cross-reference], and [cross-reference], we studied the problem of moving pictures across a canvas. The problem had two parts: moving individual shapes and moving a picture, which is a list of shapes. For the first part, we need functions to draw, clear, and translate a shape. For the second part, we need functions that draw all shapes on a list, that clear all shapes on a list, and that translate all shapes on a list. Even the most cursory look at the functions shows many repetitions. The following exercises aim to eliminate these repetitions via manual abstraction and Scheme's built-in operations.


Exercises

Exercise 21.4.1

Abstract the functions draw-circle and clear-circle into a single function process-circle.

Define translate-circle using process-shape. Hint: If a primitive function doesn't quite fit an abstraction, we have to define auxiliary functions. For now, use define to do so. Intermezzo 4 introduces a handy and important short-hand for that purpose. Solution

Exercise 21.4.2

Abstract the functions draw-rectangle and clear-rectangle into a single function process-rectangle.

Define translate-rectangle using process-shapeSolution

Exercise 21.4.3

Abstract the functions draw-shape and clear-shape into a single function process-shape. Compare the function with the template fun-for-shape.

Define translate-shape using process-shapeSolution

Exercise 21.4.4

Use Scheme's map and andmap to define draw-losh, clear-losh, and translate-loshSolution


lunar

Figure: The Apollo 11 lunar lander

NASA: National Space Science Data Center


Exercise 21.4.5

Modify the functions of exercises [cross-reference] and [cross-reference] so that pictures move up and down on a canvas.

Modify the functions so that a shape can also be a line with a start position, an end position, and a color.

Create a lunar lander picture (see figure [cross-reference]) using a list of rectangles, circles, and lines.

Develop the program lunar-lander, which shows the drop of a lunar lander from a certain height. More specifically, the program creates a canvas and moves the lunar lander from the top to the bottom.

Use the teachpack arrow.ss to give users control over how fast and when the lunar lander should move:

(start 500 100)
(draw LUNAR) 
(control-up-down LUNAR 10 lunar-lander) 
If time permits, modify the function so that a player can move the lander up, down, left or right. Use controller from arrow.ss to control the movements. Solution


[previous] [up] [next]     [index]
Next: Note: Designing Abstractions from Up: Designing Abstractions from Examples Previous: Abstraction and a Single

PLT