7. Review 2
Review of signatures, random numbers, random functions, math.
Signatures
Remember that when you are describing a model which is a string, you should write model(string) instead of just model or string. This makes it clear that you know exactly the role that value plays in the animation.
Triangle in the corner
You are making an animation that shows a triangle in the upper left corner of a 400x200 rectangle, filled in to the location of the mouse.
- What is a good model?
- What is the signature for the mouse-handler?
- What is the signature for the draw-handler?
- You add a feature so hitting a key resets the animation to show shading that just touches the lower left. (a) What is the signature for the key handler? (b) Write code for the key handler.
Countdown
An animation shows a number in the middle of a rectangle. The number starts at 60 and goes down one every second.
- Describe a good choice for a model. (What type?)
- What is the signature for the tick-handler?
- What is the signature for the draw-handler?
Random Numbers
Choices
- What expression randomly chooses -1, 0, or 1? (With all equally likely.)
- Write an expression that randomly chooses a number from -18 to 18.
- Write an expression to create a random multiple of 100, from 2000 to 3000.
- Write a function that takes in a number $m$ and puts out a random number between 10 and $m$. Bonus: If $m$ is less than 10, always put out 10.
- (Harder.) Write a function that gives a random multiple of 3 between 81 and 228.
- (This problem was very hard, consider it AP Racket.) Write a function that takes in a number $n$ and puts out a random number between $-10n$ and $50n$.
Random vs Nonrandom
Create a rectangle with a random width from 100 to 400 and a random height from 200 to 300. Put a circle in the middle of the rectangle that has half the height of the rectangle.
Math
- Write a Racket function to compute $g(x) = \sqrt{7-3x}$.
- Write a function that takes in a number and puts out a number that is between 20 and 200 by taking numbers smaller than 20 and making them 20, and numbers bigger than 200 become 200, and numbers in the middle do not change.