Graphics Primitives

NOTE: The responses to some of the questions are similar to those presented on Casey's web page, since we answered them together.

Part 1: The Graphics Environment

At the top level, the graphics environment consists one or more Graphics Context structures, much like X-Windows. This structure contains the current pen color, the current fill color, and a pointer to an Image_t object. Within the Image_t object, there are long integers representing the height and width of the image, a pointer to an array of Pixels the size of heightxwidth, and a long integer representing the number of colors in the image (usually 255). Other structures in the environment include a Point_t structure containing two long integers that represent the row and column of the desired point, and a Color_t structure that is, in essence, a Pixel.
There are several simple methods included in the graphics environment. The first is a constructor method that creates and initializes a new Graphics Context. The constructor takes in the desired height and width of the image within the Graphics Context. Sufficient memory is allocated for an image of size widthxheight, the width and height are set, the pen color is initialized to black, and the fill color is initialized to white. This method should be a clue that we are leaning toward object oriented programming and will probably port our code over to C++.
Another useful method in the Graphics Environment is SetPixel. This simply takes a row, column and Graphics Context as inputs and plots the pixel at (col, row) with the current pen color. Bounds checking is performed within this function. SetFillPixel, a variation of SetPixel, plots the pixel at (col, row) with the current fill color.
Other methods include:

Finally, the GraphicsIncludes.h file contains a list of the libraries necessary for the graphics system. This list will be expanded as more libraries are added.

Part 2: Bresenham's Line-drawing Algorithm

The algorithm selected to draw lines in the graphics environment was Bresenham's Integer Algorithm, as outlined in Rogers. The algorithm was adjusted to use a coordinate system centered in the lower left-hand corner of a pixel rather than in the center. The drawLine function also includes special cases for horizontal and vertical lines to take into account the peculiarities that arise when drawing polygons. These special cases will draw the polygons counterclockwise. The speed of drawLine was clocked on a 440 MHz Sun Ultra 10 from the Sun lab and was found to be about 19000 lines per second. The average line length was 103.9.


As an extension, we added a drawDashedLine function to draw a dashed line. This function takes in the frequency with which a pixel should be skipped, along with the arguments for drawLine, and draws the line accordingly. An algorithm to speed up the line drawing process was also implemented. The algorithm consists of drawing the line from both ends at once, using the values at one end to create the other end. An increase to 21000 lines per second was experienced. This was surprisingly less than the 38000 lines per second expected.

Part 3: The Midpoint Circle and Ellipse Algorithms

The circle algorithm selected for this task is related to the Bresenham line algorithm. Details of both the circle and ellipse creation algorithms can be found in Rogers. The circle algorithm only needs to be calculated in one octant, since a circle is perfectly symmetric. The circle is calculated in the sixth octant to adjust for the coordinate system shift. Likewise, the ellipse is calculated in the third quadrant.

Part 4: Image of a Box on a Table

This is an image of a box on a table. There isn't much more to say. It will probably be cooler after the next lab.



Questions

1. Who did you work with on this assignment, and what tasks did each of you do?
I worked with Casey Smith. We both worked on all of the tasks.

2. Describe your graphics environment. Include the following information in your answer:
Our graphics environment resembles X-Windows. To begin, the user creates a graphics context, which is a structure containing all of the variables necessary to create an image and the figures within it. In order to add primitives to the graphics context, the user calls a function which takes as arguments primitive-specific parameters (location, radius, etc) and a graphics context. It then uses the state variables from the graphics context (color, image array, etc) to draw the primitive. The graphics context is passed to each of these functions. 3. Is your first required image consistent with how you match screen coordinates to the true mathematical lines? Why, why not?
Yes. In adjusting lines to match a coordinate system centered in the lower left-hand corner of the pixel, rather than the center of the pixel, we made our lines match true mathematical lines. All lines are drawn in the first or second quadrant, don't draw the last pixel, and move the starting point if the line is in the second quadrant to start on the correct side of the coordinate axis.

4. If you extended this assignment in any way, describe what you did and how you did it. Include pictures to support your description.