Project 2 Function Plotter Mon, Sep 22, 2003 Due Mon, Sep 29, 2003
Function Plotter Read the handout.handout Download the files. FunctionPlotter.cpp. FunctionPlotter.cpp point2.h. point2.h Run the demo.demo
Function Plotter Your job is to write most of the callback functions. display() reshape() mouse() keyboard() special()
The Display Function This function will draw the graphs of the functions. Show each function from xmin to xmax. Show a function only if it is selected to be shown (see func[i].show). Draw the function in the appropriate color (see func[i].color[3]).
The Reshape Function When the window is resized, you must compute new values for xmax and ymin. Recall that the upper-left corner remains fixed when the window is resized. Therefore, xmin and ymax remain fixed.
The Mouse Function When the user right-clicks, the window should be re-centered on the point on which he clicked. You must compute new values for xmin, xmax, ymin, and ymax. This changes the boundaries, but not the scale.
The Keyboard Function When the user presses ‘+’ (or shift-‘+’), the program zooms in towards the center point. When the user presses ‘–’ (or shift-‘–’), the program zooms out from the center point. In either case, the center point remains fixed. You must compute new values for xmin, xmax, ymin, and ymax. These operations change the boundaries and the scale.
The Special Function When the user presses the arrow keys, the program should shift the window up, down, left, or right. If you shift up or down, you must compute new values for ymin and ymax. If you shift left or right, you must compute new values for xmin and xmax. These operations change the boundaries, but not the scale.
The Special Function Pressing the up arrow key should move the “viewpoint” up. Therefore, the graph will move down. Similarly for the other arrow keys. This is consistent with scrolling in other applications.
The Function Values The function values are stored in a two- dimensional array p of points. func[i].pt[j] is the j-th point of the i-th function. Thus, the x- and y-coordinates are func[i].pt[j].x and func[i].pt[j].y. The values are computed in a function called computeFuncs().
The Function Values It is your responsibility to call on computeFuncs() as necessary. Generally, whenever xmin, xmax, ymin, or ymax changes, you need to recompute the function values.
The Functions to be Plotted Ten functions are provided already, but you are free to change them. You should stick with functions whose domain is all real numbers since the program makes no attempt to determine ahead of time whether f(x) can be computed for a given x. For example, f(x) = 1/x might cause trouble if x = 0.
The Array of Function Structs The program creates an array of structures (structs) to hold the function information. We cannot store a function itself in a struct, so we must store a pointer to the function.
The Array of Function Structs The array of structs is defined as struct { float (*f)(float); Point2* pt; float color[3]; bool show; } func[NUM_FUNCS];
The Array of Function Structs It is initialized as { {sin, NULL, {1, 0, 0}, true}, {cos, NULL, {0, 1, 0}, false}, {sinPlusCos, NULL, {0, 0, 1}, false}, {twoSin2x, NULL, {0, 1, 1}, false}, {cubic, NULL, {1, 0, 1}, false}, {piecewise, NULL, {1, 0.5, 0}, false}, {rational, NULL, {0, 0.5, 0}, false}, {rational2, NULL, {0.9, 0.9, 0}, false}, {cosh, NULL, {.68,.56,.25}, false}, {sinh, NULL, {0, 0, 0}, false} };