Lab 5: Unions of structures
You may submit this lab to lab5 on Handin (this is optional).
Important: Whenever you write a function in this class, follow the design recipe. You will be graded accordingly.
In this lab, we will extend the point-painting program from lecture this week to a program which can plot zero, one, or two points.
1 Definition of a FewPoints
; A Point is (make-point Number Number) (define-struct point [x y]) ; A FewPoints is one of: ; - (make-none) ; - (make-one Point) ; - (make-two Point Point) (define-struct none []) (define-struct one [first]) (define-struct two [first second])
Exercise 1. Define three examples of FewPoints. Each example should contain a different number of points.
Exercise 2. Define the template for processing a FewPoints.
2 Drawing a FewPoints
Exercise 3. Define two images, marker and background. Sidenote: One way to control the transparency of a shape is by passing an integer between 0 and 255 as the mode argument of the shape. For example, the code for one of the circles above is (circle 40 170 "darkcyan").
Exercise 4. Design a function draw-points which takes a FewPoints and draws a picture of them. Be sure to use the examples you just defined in your tests and to use the template you just defined in your definition.
3 Changing a FewPoints
If the input contains no points, then return exactly one point whose coordinates are those of the MouseEvent.
If the input contains exactly one point, then return two points, the first of which has the coordinates of the MouseEvent and the second of which is the input point.
If the input contains two points, then return two points, the first of which has the coordinates of the MouseEvent and the second of which is the first input point.
Be sure to write check-expects which reflect all of the behavior just outlined before defining the function (and use your examples from the first section to do so).
Exercise 6. Try out add-point by using it as the mouse handler (along with (make-none) and draw-points) in a call to big-bang. Comment it out when you are done.
Exercise 7. Design a function remove-point which takes a FewPoints and a KeyEvent and updates the input FewPoints as follows.
If the input contains no points or one point, the output should contain no points.
If the input contains two points, the output should contain only the second point. In other words, a call to remove-point should undo a previous call to add-point.
The KeyEvent must be "r"; otherwise nothing changes.
Exercise 8. Try out remove-point by using it as the key handler (along with (make-none), draw-points, and add-point) in a call to big-bang.
4 Distinguishing between a FewPoints
Exercise 9. Modify your program so that the most recently added point and the previously added point appear with different colors.