Graphs

Screen of the graph drawer program, with cubic function displayed.

 

It is very common to present mathematical, engineering and financial information graphically. We will now look at a program to draw mathematical functions. Suppose we want to draw the function:

y = ax2 + bx2 + cx + d

with values for a, b, c, and d input via scrollbars.

We must resolve several design issues. First we want to see the graph with the y coordinate going up the screen, whereas y pixel coordinates measure downwards. We will distinguish between x and its equivalent pixel coordinate xPixel, and between y and yPixel.

Next we have to ensure that the graph will fit conveniently on the screen, that is not too small to see or too big to fit. Solving this problem is called scaling. We will assume that the available area on the screen is 400 pixels in the x-direction and 400 pixels in the y-direction.

Finally, since we will be using drawLine to draw the graph, we will have top draw a curved shape as a large number of small lines. We will move along the x-direction, one pixel at a time, drawing a line from the equivalent y coordinate to the next.

Hosted by www.Geocities.ws

1