## CS 1410-20 Homework 1

Due: Friday, September 2nd, 2011 10:45am

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.

### Part 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.

### Part 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.

### Part 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.

### Part 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.

### Part 5 – Squashing Ratio During a Fall

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.

### Part 6 – 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.

### Part 7 – Seconds to Scene

Implement a 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).

### Part 8 – Drop a Duck

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 instead of seconds, where each tick is 1/28 of a second.

Implement a function duck-scene 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)`

 Last update: Friday, November 4th, 2011mflatt@cs.utah.edu