On this page:
1 Horizontal tracing
2 Line tracing
3 Parabola tracing
4 Extra credit

Problem Set 2: Tracing polynomials

Submit this problem set to ps2 on Handin.

Note: Whenever you write a function in this class, you must follow the steps outlined in Lab 2: Defining functions. In particular, you must include a signature, a purpose statement and at least two tests per function.

1 Horizontal tracing

Problem 1: Define the following constants:
  • an Image named background, which should be at least 300 by 300 pixels

  • a Number named x1, which should be between 0 and the width of background

  • a Number named y1, which should be between 0 and the height of background

Problem 2: Design a function marker which takes a String, which is assumed to be a valid color, and returns a small solid shape of your choice with the given input color.

Problem 3: Pick a color. Design a function trace-horizontal which takes an x-coordinate and returns an image of marker applied to your choice of color positioned on background at the given x-coordinate and y1.

Problem 4: Pick a color. Design a function trace-horizontal-with-point which takes an x-coordinate and returns an image of marker applied to your choice of color positioned at (x1, y1) on top of trace-horizontal applied to the given x-coordinate.

Problem 5: You will now get to watch trace-horizontal-with-point with its input, starting a 0, changing over time. Add the following line to your Definitions Window:

(require 2htdp/universe)

Running DrRacket with this line will make programs available which allow us to design interactive animations. Next, add the following line to your Definitions Window:

(animate trace-horizontal-with-point)

When you run your definitions, you should see a new window appear with a point moving across the background. For example, in the picture above, the black marker would start at the left side of the background and move right, passing behind the red marker. Close the window when you are finished watching.

Finally, before moving on to the next section, comment out the line you just added which calls animate.

2 Line tracing

Problem 6: Define the following constants:
  • a Number not equal to x1 named x2, which should be between 0 and the width of background

  • a Number named y2, which should be between 0 and the height of background

Problem 7: Design a function line-y which takes an x-coordinate and computes the corresponding y-coordinate on the line determined by the points (x1, y1) and (x2, y2). Here is a formula to compute the y-coordinate:

y(x) = \frac{(y_1 - y_2) x + (x_1y_2 - x_2y_1)}{(x_1 - x_2)}

Important: Your definition of line-y must refer to constants by name wherever possible. For example, even if y1 is defined to be 50, translate y_1 as "y1" and not as 50.

Here are some tests:

(check-expect (line-y x1) y1)
(check-expect (line-y x2) y2)

Problem 8: Design a function trace-line which takes an x-coordinate and returns an image of marker applied to your choice of color positioned on background at the given x-coordinate and y-coordinate computed with line-y.

Problem 9: Design a function trace-line-with-points which takes an x-coordinate and returns images of marker applied to your choice of color positioned at (x1, y1) and at (x2, y2) on top of trace-line applied to the given x-coordinate.

Problem 10: Apply animate to trace-line-with-points. You should see a marker moving in a line behind (x1, y1) and (x2, y2).

Before moving on to the next section, comment out the line you just added which calls animate on trace-line-with-points.

3 Parabola tracing

Problem 11: Define the following constants:
  • a Number not equal to x1 or x2 named x3, which should be between 0 and the width of background

  • a Number named y3, which should be between 0 and the height of background

  • a Number named c1 equal to

    \frac{x_1 (y_3 - y_2) + x_2 (y_1 - y_3) + x_3 (y_2 - y_1)} {(x_1 - x_2)(x_1 - x_3)(x_2 - x_3)}

  • a Number named c2 equal to

    \frac {x_1^2 (y_2 - y_3) + x_2^2 (y_3 - y_1) + x_3^2 (y_1 - y_2)} {(x_1 - x_2)(x_1 - x_3)(x_2 - x_3)}

  • a Number named c3 equal to

    \frac {x_1 x_2 y_3 (x_1 - x_2) + x_1 y_2 x_3 (x_3 - x_1) + y_1 x_2 x_3 (x_2 - x_3)} {(x_1 - x_2)(x_1 - x_3)(x_2 - x_3)}

Important: Your definitions of c1, c2, and c3 must refer to constants by name wherever possible. For example, even if y1 is defined to be 50, translate y_1 as "y1" and not as 50.

Problem 12: Design a function parabola-y which takes an x-coordinate and computes the corresponding y-coordinate on the parabola determined by the points (x1, y1), (x2, y2), and (x3, y3). Here is a formula to compute the y-coordinate:

y(x) = c_1 x^2 + c_2 x + c_3

Here are some tests:

(check-expect (parabola-y x1) y1)
(check-expect (parabola-y x2) y2)
(check-expect (parabola-y x3) y3)

Problem 13: Design a function trace-parabola which takes an x-coordinate and returns an image of marker applied to your choice of color positioned on background at the given x-coordinate and y-coordinate computed with parabola-y.

Problem 14: Design a function trace-parabola-with-points which takes an x-coordinate and returns images of marker applied to your choice of color positioned at (x1, y1), at (x2, y2), and at (x3, y3) on top of trace-parabola applied to the given x-coordinate.

Problem 15: Apply animate to trace-parabola-with-points. You should see a marker moving in a curve behind (x1, y1), (x2, y2), and (x3, y3).

4 Extra credit

How might we control the tracing speed? Define a constant speed. To start with, it should be a number near one. A speed between 0 and 1 should slow the tracing down; a speed greater than 1 should make the tracing faster. Redefine either trace-line or trace-parabola so that it is controlled by speed.