2-D Graphics Primitives - Writeup
Phil Katz and Stephen St.Vincent
home   |   images
Lab description


Lab summary:

The purpose of this lab was to create a generalized graphics library in either C or C++. We chose to write our library in C. The first task was to create an Image structure capable of reading image files (PPMs, specifically), storing them in memory, and writing to PPM files. This task was accomplished relatively easily.

The next task was to create various image primitives. These were: Point; Line; Circle; and Ellipse. All of these built upon the Pixel primitive defined in C. We implemented Bresenham's algorithm ( http://en.wikipedia.org/wiki/Bresenham%27s_algorithm) to perform line-drawing. One important note was that this did not cover vertical and horizontal lines, which were implemented separately (see question 3 below).

Circles and ellipses were drawn by creating and algorithm to draw the portion of a cicle that lies in one octant (half a quadrant) of the Cartesian plane, then rotating and reflecting to achieve a full circle. Again, special care was taken to ensure that these shapes did not exceed their radii. So, for example, a circle of defined radius 20 will span 19 pixels (inclusive).

On a 3.0 GHz Intel Pentium 4, our algorithm was able to draw 300,550 lines per second with 102.9 pixels per line. Before we inlined most of our library functions, we were achieving values around 100,000 lines per second (with comparable pixels/line). This massive speedup shows the power and usefulness of inlining small but frequently-used functions.

Questions:
  1. Who did you work with on this assignment, and what tasks did each of you do?
    Phil and Steve were partners on this assignment. We mostly worked together, although specifically Steve was responsible for the relatively easy Circle and Ellipse structs as well as writing the API. Phil handled the more difficult Line struct.

  2. Develop an API manual for your graphics environment.
    Link to API: graphicsAPI.pdf

  3. What modifications to the code did you make in order to deal with coordinate system issues such as making lines and circles the proper theoretical size?
    To make circles and ellipses the proper sizes, we left the drawing algorithm in the first quadrant but subtracted 1 from each relevant radius. In this way, a circle of radius 10 would span 19 pixels (inclusive).

    In order to make the vertical and horizontal lines be in the proper columns/rows, we had to draw upward vertical lines left of the starting point, downward vertical lines right of the starting point, leftward horizontal lines below the starting point, and rightward horizontal lines above the starting point.