CPSC 324: Computer Graphics, Spring 2001
First Programming Assignment

Your first real programming assignment is to improve the simple drawing program, draw1_double.cc. Currently, the only thing that this program can draw is red lines. But it does have a Clear button that removes all lines and an Undo button that removes the most recently added line. Your improved program will offer a choice of drawing lines, rectangles, or filled rectangles. It will also offer a choice of drawing color.

This assignment is due in class on Tuesday, February 20.

You should have buttons for choosing between the three possible types of figures, lines, rects, and filled rects. After the user clicks on one of these buttons, the user can draw multiple figures of the selected type. Your program will need a global variable to keep track of which type of figure is being drawn. Note that the button might show an icon of the associated figure, rather than a word.

Similarly, the user should be able to select a drawing color. You could make this possible, for example, by having a "palette" of little buttons that show all the available colors. The user selects a color by clicking on the button of that color. After a color is selected, all the drawing that user does will be in that color until the user selects a new color. You will need a global variable to keep track of the current drawing color.

In the draw1_double.cc program, information about the lines that have been drawn is stored in four arrays of type float[] named x1, y1, x2, and y2. The two endpoints for the i-th line are (x1[i],y1[i]) and (x2[i],y2[i]). The information in these arrays is enough to completely redraw the picture in the display() function. Since your program will allow various colors and types of figures, you will need to save more information for each figure. Note that the geometry of a rectangle can still be specified by two points, namely points at opposite corners of the rectangle, so you don't need to store additional coordinates. However, you will have to store the type and color of each figure. The type can be stored in an array of int. If you are using a color palette, the color can also be specified as the int that gives the index of the color in the palette. If you implement RGB colors, as described below, you could use three arrays of type float for the red, green, and blue components of the color.

(With all this data floating around, it would really be better to organize it into an array of struct's (or objects), where a struct holds all the data for a given figure. For example, you could define:

      struct FigureData {
         float x1, y1;   // coordinates of one point
         float x2, y2;   // coordinates of other point
         int type;       // code for type of figure
         int colorIndex; // index of color in palette

However, it is not required that you do this. If you would like to be more object-oriented in your programming, you could even use an array of objects, where an object contains information about the figure as well as a method for drawing the figure.)

If you implement the program as described so far, you can get a maximum of 82% (a grade of B-). For a higher grade, add some of the following options, or other options that you think up on your own:

David Eck, February 5, 2001