Bresenham s Line Generation The Bresenham algorithm is another incremental scan conversion algorithm. The big advantage of this algorithm is that, it uses only integer calculations. Difference Between DDA Line Drawing Algorithm and Bresenhams Line Drawing Algorithm. Digital Differential Analyzer Bresenhams Line Drawing Algorithm Arithmetic Operations DDA algorithm uses floating Bresenhams algorithm uses fixed points points i.e. Real Arithmetic. i.e. Integer Arithmetic. DDA algorithm uses Bresenhams algorithm uses only multiplication and division in its subtraction and addition in its operations. operations. 1
Speed DDA algorithm is rather slowly Bresenhams algorithm is faster than DDA than Bresenhams algorithm in algorithm in line drawing because it line drawing because it uses real performs only addition and subtraction in arithmetic (floating-point its calculation and uses only integer operations). arithmetic so it runs significantly faster. DDA algorithm is not as accurate Accuracy & Bresenhams algorithm is more efficient and efficient as Bresenham Efficiency and much accurate than DDA algorithm. algorithm. Drawing Round Off Expensive DDA algorithm can draw circles Bresenhams algorithm can draw circles and curves but that are not as and curves with much more accuracy than accurate as Bresenhams DDA algorithm. algorithm. DDA algorithm round off the Bresenhams algorithm does not round off coordinates to integer that is but takes the incremental value in its nearest to the line. operation. DDA algorithm uses an Bresenhams algorithm is less expensive enormous number of floatingpoint multiplications so it is than DDA algorithm as it uses only addition and subtraction. expensive. Moving across the x axis in unit intervals and at each step choose between two different y coordinates. For example, as shown in the following illustration, from position (2, 3) you need to choose between (3, 3) and (3, 4). You would like the point that is closer to the original line. 2
At sample position Xk+1, the vertical separations from the mathematical line are labeled as dupper and dlower. The pixel positions on line can be identified by doing sampling at unit x intervals. The process of sampling begins from the pixel position (X0,Y0) and proceeds by plotting the pixels whose Y value is nearest to the line path. If the pixel to be displayed occurs at a position (X k, Y k ) then the next pixel is either at (X k +1,Y k ) or (X k +1,Y k +1) i.e, (3,2) or (3,3) The Y coordinate at the pixel position X k +1 can be obtained from Y=m(X k +1)+b........... eq 1 the separation between (X k+1,y k ) and (X k+1,y) is d1 and the separation between (X k+1,y) and (X k+1, Y k+1 ) is d2 then d1 = y y k and d2 = (Y k +1) Y 3
(X k, y k +1) (X k +1, y k +1) d 2 P 0 d 1 (X k, y k ) (X k +1,y k ) Y=m(X k +1)+b........ eq 1 d1=y y k d1=m(x k +1)+b Y k ( from eq (1))..( 2) And d2= (Y k +1) Y =(Y K +1) [m(x k +1)+b] =(Y K +1) m(x k +1) b.(3) The difference is given as d1-d2 = m(x k +1)+b-Y k -[(Y k +1)-m(X k +1)-b] =m(x k +1)+b-Y k -(Y k +1)+m(X k +1)+b =m(x k +1)+b-Y k -Y k -1+m(X k +1)+b d1-d2 = 2m(X k +1)-2Y k +2b 1 (4) A decision parameter P k can be obtained by substituting m= dy/dx in equation 4 = 2 dy/dx (X k +1) 2Y k + 2b 1 = (2 dy(x k +1)-2 dx.y k + 2b.dx -dx )/dx dx(d1-d2) = 2 dy(x k +1)-2 dx.y k + 2b.dx - dx = 2 dyx k +2 dy-2 dx.y k + 2b.dx -dx 4
= 2 dyx k - 2 dx.y k + c where c=2dy+2b.dx-dx Where, dx(d1-d2) = Pk and c= 2 dy+ dx(2b-1) Pk = 2 dyx k - 2 dx.y k + c................... (5 ) The value of c is constant and is independent of the pixel position. It can be deleted in the recursive calculations, of for P k if d1 < d2 (i.e, Yk is nearer to the line path than Yk+1) then, P k is negative, then a lower pixel (Y k ) is plotted else, an upper pixel (Y k +1) is plotted. At k+1 step, the value of Pk is given as P K+1 = 2 dyx k+1-2 dx.y k+1 + c..(6 ) (from 5) Eq 6 eq 5 Pk+1 Pk = (2 dyx k+1-2 dx.y k+1 + c ) - (2 dyx k +2 dx.y k + c) = 2dy(X k+1 -X k ) 2 dx(y k+1 Y k ).(7) Since X k+1 = X k +1 The eqn 7 becomes Pk+1 Pk = 2dy(X k +1 -X k ) 2 dx(y k+1 Y k ) = 2dy - 2 dx(y k+1 Y k ) Pk+1 = Pk + 2dy - 2 dx(y k+1 Y k )..( 8) Where (Yk+1 Yk) is either 0 or 1 based on the sign of Pk If 0 then pk+1=pk+2dy If 1 then pk+1=pk+2dy-2dx. The starting parameter P0 at the pixel position (X0,Y0) is given as P0 = 2dy dx (9) Bresenham s algorithm 5
Step 1: Enter the 2 end points for a line and store the left end point in (X0,Y0). Step 2: Plot the first point be loading (X0,Y0) in the frame buffer. Setp 3: determine the initial value of the decision parameter by calculating the constants dx, dy, 2dy and 2dy-2dx as dx=x1-x0, dy=y1-y0, P0 = 2dy dx Step 4: for each Xk, conduct the following test, starting from k= 0 If Pk <0, then the next point to be plotted is at (Xk+1, Yk) and, Pk+1 = Pk + 2dy Else, the next point is (Xk+1, Yk+1) and Pk+1 = Pk + 2dy 2dx (step 3) Step 5: iterate through step (4) dx times. Or Step 1: Enter the 2 end points for a line (X0,Y0), (X1,Y1). Step 2: Plot (X0,Y0,colo). Setp 3: determine the initial value of the decision parameter by calculating the constants dx=x1-x0, dy=y1-y0, P0 = 2dy dx for k=0 to dx do If Pk <0, then the next point to be plotted is at (Xk+1, Yk) Pk+1 = Pk + 2dy Else the next point is (Xk+1, Yk+1) Pk+1 = Pk + 2dy 2dx Endfor 6
Example Let the given end points for the line be (30,20) and (40, 28) dy = y1 y0 = 28 20 = 8 dx = x1 x0 = 40 30 = 10 m = 0.8 The initial decision parameter P0 is P0 = 2dy dx = 2(8) 10 = 16 10 = 6 The constants 2dy and 2dy-2dx are 2dy = 2(8) = 16 2dy-2dx = 2(8)- 2(10) =16 20=-4 The starting point (x0, y0)=(30,20) and the successive pixel positions are given in the following table K P k (X, Y ) k+1 k+1 0 6 (31,21) 1 2 (32,22 2-2 33,22 3 14 34,23 4 10 35,24 5 6 36,25 7
6 2 37,26 7-2 38,26 8 14 39,27 9 10 40,28 8