These problems continue directly from problem set 5.
A falling cartoon duck stays its normal shape until it hits the ground. Implement the function current-squash, which takes two numbers: an elapsed time in seconds and the distance to the ground in pixels from the duck’s starting point. Compute the duck’s squashing ratio at the given time. The ratio will be 1 if the duck has not yet reached the ground. If the duck has hit the ground by the given time, then the ratio is determined by the velocity of the duck at the time when it hit the ground.
For example, (current-squash 2 400) should produce 1/2.
Don’t forget to use functions that you’ve already written if they can help.
Implement the duck-scene/seconds function that takes a number representing an elapsed time in seconds and returns an image. The returned image should be one produced by place-duck with the bottom of the duck down from the top of the scene by as many pixels as distance produces for the elapsed time—but no further down than the bottom of the scene. Furthermore, if the duck has reached the bottom of the scene, then it should be suitably squashed.
The scale/xy function from 2htdp/image can be used for squashing a duck. Use 1 for the first argument and a squashing ratio for the second argument.
Although generating images by hand would be difficult for testing, you can at least check that (duck-scene/seconds 1) produces the same result as (place-duck duck 100), and check that (duck-scene/seconds 10) produces the same result as (place-duck (scale/xy 1 1/2 duck) 400).
The animate form of 2htdp/universe creates a kind of movie given a function from a number to a scene. The function receives a number in ticks, where each tick is 1/28 of a second.
Implement a function duck-scene/ticks that is like duck-scene/seconds, but that takes a number in ticks instead of seconds. (Your duck-scene function should mostly just call your duck-scene/seconds function.)
In DrRacket’s interactions window (not your program), run the movie with
(animate duck-scene)