Problems for Lesson 5

If you drop a cartoon duck in a frictionless cartoon environment, it will move ever more quickly toward the cartoon ground with constant acceleration—until it hits the ground, at which point it will sqaush by an amount inversely related to the duck’s speed.

Assume that the duck starts at rest and that acceleration is 200 pixels per second per second.

Problem 5.1 – Velocity

Implement the function velocity, which takes a number representing an elapsed time in seconds and returns a number representing a dropped duck’s velocity in pixels per second. Assume that the duck has not hit the ground by the given time. Recall that velocity is acceleration multiplied by time.

For example, (velocity 1) should produce 200.

Problem 5.2 – Distance

Implement the function distance, which takes a number representing an elapsed time in seconds and returns the number of pixels that a dropped duck has moved. Assume that the duck has not hit the ground by the given time. Recall that distance is one-half of acceleration multiplied by time squared.

For example, (distance 1) should produce 100.

Problem 5.3 – Time to Impact

Implement the function time, which takes a number representing a distance to the ground in pixels (from the duck’s starting point) and returns a number representing an elapsed time in seconds required for the duck to hit the ground. Assume that the duck starts more than the given distance from the ground. Recall that time is the square root of twice distance divided by acceleration.

For example, (time 100) should produce 1.

Problem 5.4 – Squashing Ratio

Implement the function squash, which takes a number representing a velocity in pixels per second and returns the ratio of a duck’s squashed size to its original size when after it hits the ground with the given velocity. The ratio is determined as follows: divide the velocity by 400 pixels per second, add one to that result, and then take the multiplicative inverse (i.e., divide 1 by one more than the velocity divided by 400).

For example, (squash 400) should produce 1/2.

Problem 5.5 – Making a Scene with a Duck

Implement a place-duck function that takes an image and a number to produce a scene image. The given image represents a duck, and the number represents the number of pixels that the duck has fallen; specifically, the bottom edge of the duck image should the given number of pixels from the result scene’s top. The duck should be centered hoziontally in the scene, and the scene should be 150 pixels wide and 400 pixels tall.

In addition to (require 2htdp/image), use (require 2htdp/universe) to get the empty-scene and place-image functions. Use empty-scene to create the 150-by-400 scene, and use place-image to add the duck in the scene. Note that the place-image function takes numbers to position the center of the duck, so if you want to put a duck’s bottom edge at position y in a scene, then subtract half of the duck image’s height from y to get the third argument for place-image.

Here’s a duck to use:

Writing test cases for functions that produce images is difficult at best, since it’s difficult to produce the right image by hand. You can skip tests for place-duck.