Problem Set 5: Painting many points
Submit this assignment to ps5 on Handin.
Note: Whenever you write a function in this class, follow the design recipe. You will be graded accordingly.
1 Complete Lab 5: Unions of structures
Problem 1. Complete Exercises 3, 4, 5, 7, and 9. Of course, you may use the work you did on these exercises in lab as a basis for your solution to this problem.
2 Developing a data definition
In this problem set, you must develop your own data definition and your own structures. You must not use any built-in structures.
Variations of some of the problems in this problem set are done in Week 6’s lecture. Please review the code from lecture as you work the problems. Sometimes your solutions will look similar to the code from lecture; this is fine.
Here is a sample problem and a solution.
Sample problem. Develop a data definition NestOfNumbers which can store arbitrarily many numbers. Define corresponding structures named no-numbers and some-numbers. Define three examples of your data definition. Define a template for processing a NestOfNumbers.
; A NestOfNumbers is one of: ; - (make-no-numbers) ; - (make-some-numbers Number NestOfNumbers) (define-struct no-numbers []) (define-struct some-numbers [first rest]) (define ns1 (make-no-numbers)) (define ns2 (make-some-numbers 211 (make-no-numbers))) (define ns3 (make-some-numbers 37 ns2)) ; process-non : NestOfNumbers -> ... (define (process-non non) (cond [(no-numbers? non) (...)] [(some-numbers? non) (... (some-numbers-first non) ... (process-non (some-numbers-rest non)) ...)]))
3 draw-pairs
Problem 2. Develop a data definition named Pair and a structure named pair for storing a pair of numbers representing x and y coordinates. Define three examples of your data definition.
Problem 3. Develop a data definition PackOfPairs which can store arbitrarily many pairs of numbers using the data definition from the previous problem. Name the structures you define no-pairs and some-pairs. Define three examples of your data definition.
Problem 4. Write the templates for both data definitions you have developed, called process-pair and process-pairs.
Problem 5. Choose a marker and and a background. Design a function called draw-pairs, which, given a PackOfPairs, visualizes each of the pairs as the marker at location (x,y) (obtained via pair-x and pair-y) on the background.
4 Paint programs
In the following problems, we will create two interactive animations with big-bang. Both will be simple paint programs which use draw-pairs from Problem 5.
In the first version, any mouse event will paint circles on the scene, and any keyboard event will undo the most recent painting operation (if there is one). In the second version, these operations will require specific mouse events, "button-down" and "drag", and a specific key event, "z".
For big-bang, a world is always a PackOfPairs.
Problem 6. Design an on-mouse function for big-bang called any-paint. Given a PackOfPairs, two numbers (for x and y coordinates) and a MouseEvent, any-paint causes big-bang to paint a new circle on the scene, no matter which MouseEvent happened. Recall that an on-mouse function returns a world: so any-paint returns a PackOfPairs.
Design also an on-key function for big-bang called any-undo. Given a PackOfPairs and a KeyEvent, any-undo causes big-bang to remove the most recently painted circle from the scene, no matter which KeyEvent happened. Recall that an on-key function returns a world: so any-undo returns a PackOfPairs.
Hint: any-undo should be sort of an inverse to any-paint. For every example you write for any-paint, you should write a corresponding example for any-undo, where you just switch the input and the output. Also, as usual, every function should have examples that cover every possible kind of input and every possible kind of output.
Finally, define a function run-any that takes a PackOfPairs as an initial world and runs big-bang with draw-pairs, any-paint and any-undo. Recall that big-bang returns the final world: so run-any returns a PackOfPairs.
Problem 7. Design an on-mouse function for big-bang called paint. Given a PackOfPairs, two numbers (for x and y coordinates) and a MouseEvent, paint does the following. If the mouse event is "button-down" or "drag", paint causes big-bang to paint a new circle on the scene. If the MouseEvent is anything else, paint causes big-bang to do nothing to the scene.
Design also an on-key function for big-bang called undo. Given a PackOfPairs and a KeyEvent, undo does the following. If the key pressed is "z", undo causes big-bang to remove the most recently painted circle from the scene. If any other key is pressed, undo causes big-bang to do nothing to the scene.
Finally, define a function run that takes a PackOfPairs as an initial world and runs big-bang with draw-pairs, paint and undo. Recall that big-bang returns the final world: so run returns a PackOfPairs.