Derivation of Bresenham's Line Algorithm

Derivation for first octant based on version in Hearn & Baker's "Computer Graphics"

Explanations and Generalizations written by Kenny Hoff
September 2, 1995


Overview of Problem

Given to line endpoints, we are trying to find the "in-between" points on a pixel grid. Bresenham's line algorithm determines subsequent points from the start point by making a decision between the two next available points by determining which is closer to the ideal point.


Derivation of Algorithm


Implementation of Algorithm for First Octant

Using the "tools" derived in the previous section it should be fairly simple to construct an algorithm that draws a line in the first octant (0 <= Slope <= 1). Before going on to the other octants, lets go through the necessary steps for this one:


Implementation of Algorithm for Four Octants with Slopes -1 to 1

This one is easy. In the previous implementation when we checked the decision variable, we always incremented x and y by ONE (POSITIVE ONE). The dX and dY values are always positive regardless of which line is chosen (with slope -1 to 1). If we keep the start point as point A, we can determine the SIGN of the values to increment by. So if Ax < Bx, we know that x will be incremented by one, BUT if Bx < Ax, we know that x will be DECREMENTED by one. The same thing for the y values: if Ay < By, y will be incremented by one; and if By < Ay, y will be decremented by one. This one is quite easy to implement with only a few adjustments in the previous implementation. Since the decision variable and its possible increments are calculated by dX and dY, and they are always positive, we ONLY need to change the sign of the increments for x and y. So in essence, we deciding subsequent points based on the calculations for the first octant only, but instead of stepping in x and y in a positive direction we are checking to determine which way we should "step". We are simply moving these four octants into the first for calculations.


Generalized Implementation for all Directions

This one is a bit more complex and requires a careful examination of the four-octant version. Since the slope was restricted from -1 to 1, we were always able to step in x by one and simply determine the next y-value. This works because there is always one y value for each x value along the line. This does not work stepping in y for these slopes since there will be more than one x value for each y along the line. With slopes -1 to 1, y is said to be dependent on x; y being the dependent variable and x being the independent variable. With slopes greater than 1 or less than -1, y becomes the independent variable.

So what now? We must check for the slope of the line: is it within our previous bounds where x is independent or is it where y is independent. Each type must be drawn differently. If the slope is within -1 and 1, then the previous implementation will suffice, but if the slope is out of this range, we must take the previous implementation and swap all x and y values to "move" the calculations back into the first octant. This includes swapping dX and dY. Care must be taken to ensure that it will step in y and only step in x if the decision variable chooses point-to-right-and-up.

This completes the generalized version of the Bresenham's line drawing algorithm. The actual coded implementation will reveal many possible efficiency considerations. I have deliberately left out my version in this document to allow an unbiased interpretation of the Bresenham derivation. In the very least, the code should have no floating point numbers, multiplication or division operations, or even any comparisons to any number other than zero (they are slower); only additions, bit-shifts, and comparisons to zero are allowed. Good Luck!


written by Kenny Hoff (hoffkx@timewarp.uncg.edu)