You are on page 1of 2

Bresenhams MidPoint Line Algorithm The DDA algorithm is inefficient because each iteration requires floating-point arithmetic and

the use of the round function. Bresenham developed a classic algorithm which uses only integer arithmetic, thus avoiding the round function, and allows calculations to be performed iteratively (i.e. (xi+1, yi+1 ) is based on (xi , yi)). The midpoint technique (an adaptation of Bresenhams algorithm) was first published by Pittaway and modified by Van Aken. Assume the line has a slope, m, between 0 and 1. Suppose the lower left end point is (x0, y0) and the upper right end point is (x1,y1). Assume we just selected the pixel (xp , yp ) and now must select between the pixel E (East) or NE (Northeast)

NE Q M (xp , yp )

Actual Line

Let Q be the intersection point of the line being scan-converted with the grid line x = xp + 1. We observe on which side of the line the midpoint M lies. If the midpoint lies above the line, E is chosen. If it lies below the line, NE is chosen. The line may pass between E and NE or both pixels may lie on one side but in either case the midpoint test selects the closest pixel. We must determine on which side of the line the midpoint lies. To do this, we observe that any line can be represented by F(x, y) = ax + by + c = 0 Let dy = y1 y0 and dx = x1 x0, then the line can also be written as y = mx + B = (dy/dx) x + B Or (dy/dx) x y + B = 0 Multiply by dx and we have (dy)x (dx)y + (dx)B = 0 So a = dy, b = -dx and c = (dx)B. If F(x, y) = 0, then (x, y) is on the line If F(x, y) > 0, then (x, y) is below the line If F(x, y) < 0, then (x, y) is above the line

To apply the midpoint criterion, we compute: F(M) = F(xp + 1, yp + ) = a (xp + 1) + b (yp + ) + c And test the sign. Let d = F(M). Then If d >0, select NE since M is below the line. If d = 0, select E arbitrarily If d < 0, select E since M is above the line. Suppose E is chosen, M is incremented by one step in the x direction and we have a new d, say dNew. dNew = F(xp + 2, yp + ) = a (xp + 2) + b (yp + ) + c dOld = F(xp + 1, yp + ) = a (xp + 1) + b (yp + ) + c thus dNew - dOld = a = dy. Thus after E is chosen, the increment to add to get the new d is a = dy. Suppose NE is chosen, M is incremented by one in both x and y and dNew = F(xp + 2, yp + 3/2) = a (xp + 2) + b (yp +3/2) + c dOld = F(xp + 1, yp + ) = a (xp + 1) + b (yp + ) + c thus dNew - dOld = a + b = dy dx. To start the algorithm, the first point (xp , yp ) = (x0, y0). To calculate the first d to start the algorithm, the first midpoint is (x0 + 1, y0 + ) and F(M) = F(x0 + 1, y0 + ) = a (x0 + 1) + b (y0 + ) + c = ax0 + by0 + c + a + b/2 =0 + a + b/2 since (x0, y0) is on the line Thus the starting d = a + b/2 = dy dx/2 To eliminate fractions, redefine F by multiplying by 2. Then F(x,y) = 2 (ax + by + c) = 0. This redefinition will cause d to become 2dy dx With this adjustment, Bresenhams midpoint algorithm becomes (for slopes 0 < m < 1)
dx = x1 x0 dy = y1 y0 d = 2*dy dx incrE = 2dy incrNE = 2 (dy dx) x = x0, y = y0 plot_point (x, y) while (x < x1) { if (d <=0) { d = d + x = x + } else { d = d + x = x + y = y + } plot_point (x, }

// the first d //increment for d if we just went E //increment for d if we just went NE

//go east incrE 1 //calculate the new d

//go northeast incrNE 1 1 y)

You might also like