To derive Bresenham's algorithm, two steps must be taken: the first is transforming the equation of a line from the typical slope-intercept form into an implicit equation with integer coefficients, and the second is using this new equation to draw a line based on the idea of accumulation of error.
Line equation The slope-intercept form of a line is written as :y = f(x) = mx + b where m is the slope and b is the
y-intercept. Because this is a function of only x, it can't represent a vertical line. Therefore, it would be useful to make this equation written as a function of both x
and y, to be able to draw lines at any angle. The angle (or slope) of a line can be stated as "rise over run", or \Delta y/\Delta x. Then, using algebraic manipulation, : \begin{align} y & = mx + b \\ y & = \frac{\Delta y}{\Delta x} x + b \\ (\Delta x) y & = (\Delta y) x + (\Delta x) b \\ 0 & = (\Delta y) x - (\Delta x) y + (\Delta x) b \end{align} Letting this last equation be a function of x and y, it can be written as :f(x,y) := Ax + By + C = 0 where the constants are • A = \Delta y = y_1 - y_0 • B = - \Delta x = - (x_1 - x_0) • C = (\Delta x) b = (x_1 - x_0) b The line is then defined for some constants A, B, and C anywhere f(x,y) = 0. That is, for any (x,y) not on the line, f(x,y) \ne 0. This form involves only integers if x and y are integers, since the constants A, B, and C are defined as integers. As an example, the line y=\frac{1}{2}x + 1 then this could be written as f(x,y) = x - 2y + 2. The point (2,2) is on the line :f(2,2) = x - 2y + 2 = (2) - 2(2) + 2 = 2 - 4 + 2 = 0 and the point (2,3) is not on the line :f(2,3) = (2) - 2(3) + 2 = 2 - 6 + 2 = -2 and neither is the point (2,1) :f(2,1) = (2) - 2(1) + 2 = 2 - 2 + 2 = 2 Notice that the points (2,1) and (2,3) are on opposite sides of the line and f(x,y) evaluates to positive or negative. A line splits a plane into halves and the half-plane that has a negative f(x,y) can be called the negative half-plane, and the other half can be called the positive half-plane. This observation is very important in the remainder of the derivation.
Algorithm The starting point is on the line :f(x_0, y_0) = 0 only because the line is defined to start and end on integer coordinates (though it is entirely reasonable to want to draw a line with non-integer end points). Keeping in mind that the slope is at most 1, the problem now presents itself as to whether the next point should be at (x_0 + 1, y_0) or (x_0 + 1, y_0 + 1). Perhaps intuitively, the point should be chosen based upon which is closer to the line at x_0 + 1. If it is closer to the former then include the former point on the line, if the latter then the latter. To answer this, evaluate the line function at the midpoint between these two points: :f(x_0 + 1, y_0 + \tfrac 1 2) If the value of this is positive then the ideal line is below the midpoint and closer to the candidate point (x_0+1,y_0+1); i.e. the y coordinate should increase. Otherwise, the ideal line passes through or above the midpoint, and the y coordinate should stay the same; in which case the point (x_0+1,y_0) is chosen. The value of the line function at this midpoint is the sole determinant of which point should be chosen. The adjacent image shows the blue point (2,2) chosen to be on the line with two candidate points in green (3,2) and (3,3). The black point (3, 2.5) is the midpoint between the two candidate points.
Algorithm for integer arithmetic Alternatively, the difference between points can be used instead of evaluating f(x,y) at midpoints. This alternative method allows for integer-only arithmetic, which is generally faster than using
floating-point arithmetic. To derive the other method, define the difference to be as follows: : D_i = f(x_i+1,y_i+\tfrac 1 2) - f(x_0,y_0) For the first decision, this formulation is equivalent to the midpoint method since f(x_0,y_0)=0 at the starting point. Simplifying this expression yields: :\begin{array}{rclcl} D_0 & = & \left[ A(x_0+1) + B \left(y_0+\frac{1}{2}\right) + C \right] & - & \left[ A x_0 + B y_0 + C \right] \\ & = & \left[ Ax_0 + B y_0+ C + A + \frac {1}{2} B\right] & - & \left[ A x_0 + B y_0 + C \right] \\ & = & A + \frac{1}{2} B = \Delta y - \frac{1}{2} \Delta x \end{array} Just as with the midpoint method, if D_0 is positive, then choose (x_0+1,y_0+1), otherwise choose (x_0+1,y_0). If (x_0+1,y_0) is chosen, the change in D_i will be: :\begin{array}{lclcl} \Delta D &=& f(x_0+2,y_0+\tfrac 1 2) - f(x_0+1,y_0+\tfrac 1 2) &=& A &=& \Delta y \\ \end{array} If (x_0+1,y_0+1) is chosen the change in D_i will be: :\begin{array}{lclcl} \Delta D &=& f(x_0+2,y_0+\tfrac 3 2) - f(x_0+1,y_0+\tfrac 1 2) &=& A+B &=& \Delta y - \Delta x \end{array} If the new D is positive then (x_0+2,y_0+1) is chosen, otherwise (x_0+2,y_0). This decision can be generalized by accumulating the error on each subsequent point. All of the derivation for the algorithm is done. One performance issue is the 1/2 factor in the initial value of D. Since all of this is about the sign of the accumulated difference, then everything can be multiplied by 2 with no consequence. This results in an algorithm that uses only integer arithmetic. plotLine(x0, y0, x1, y1) dx = x1 - x0 dy = y1 - y0 D = 2*dy - dx y = y0
for x from x0 to x1 plot(x, y)
if D > 0 y = y + 1 D = D + (2 * (dy - dx))
else D = D + 2*dy
end if Running this algorithm for f(x,y) = x-2y+2 from (0,1) to (6,4) yields the following differences with dx=6 and dy=3: D=2*3-6=0 Loop from 0 to 6 * x=0:
plot(0, 1), D≤0: D=0+6=6 * x=1:
plot(1, 1), D>0: D=6-12=-6, y=1+1=2, D=-6+6=0 * x=2:
plot(2, 2), D≤0: D=0+6=6 * x=3:
plot(3, 2), D>0: D=6-12=-6, y=2+1=3, D=-6+6=0 * x=4:
plot(4, 3), D≤0: D=0+6=6 * x=5:
plot(5, 3), D>0: D=6-12=-6, y=3+1=4, D=-6+6=0 * x=6:
plot(6, 4), D≤0: D=0+6=6 The result of this plot is shown to the right. The plotting can be viewed by plotting at the intersection of lines (blue circles) or filling in pixel boxes (yellow squares). Regardless, the plotting is the same.
All cases However, as mentioned above this only works for
octant zero, that is lines starting at the origin with a slope between 0 and 1 where x increases by exactly 1 per iteration and y increases by 0 or 1. The algorithm can be extended to cover slopes between 0 and -1 by checking whether y needs to increase or decrease (i.e. dy 0 y = y + yi D = D + (2 * (dy - dx))
else D = D + 2*dy
end if By switching the x and y axis an implementation for positive or negative steep slopes can be written as plotLineHigh(x0, y0, x1, y1) dx = x1 - x0 dy = y1 - y0 xi = 1
if dx 0 x = x + xi D = D + (2 * (dx - dy))
else D = D + 2*dx
end if A complete solution would need to detect whether x1 > x0 or y1 > y0 and reverse the input coordinates before drawing, thus plotLine(x0, y0, x1, y1)
if abs(y1 - y0) x1 plotLineLow(x1, y1, x0, y0)
else plotLineLow(x0, y0, x1, y1)
end if else if y0 > y1 plotLineHigh(x1, y1, x0, y0)
else plotLineHigh(x0, y0, x1, y1)
end if end if In low level implementations which access the video memory directly, it would be typical for the special cases of vertical and horizontal lines to be handled separately as they can be highly optimized. Some versions use Bresenham's principles of integer incremental error to perform all octant line draws, balancing the positive and negative error between the x and y coordinates. plotLine(x0, y0, x1, y1) dx = abs(x1 - x0) sx = x0 = dy
if x0 == x1
break error = error + dy x0 = x0 + sx
end if if e2 <= dx
if y0 == y1
break error = error + dx y0 = y0 + sy
end if end while ==Similar algorithms==