Numerical Examples of Bresenham’s Line Algo

Numerical Examples of Bresenham’s Line Algo

Numerical Examples of Bresenham’s Line Algo

Using Bresenham’s algorithm, generate the coordinates of the pixels that lie on a line segment having the endpoints (2, 3) and (5, 8).

Case: When slope (m) > 1

Now let’s solve the same numerical using BLA Algorithm.

S-1:  x1=2; y1=3; x2=5; y2=8.

S-2: dy=y2-y1 8-3= 5 and dx = x2-x1 = 5-2 = 3

 dy-dx = 5-3 = 2; and 2 * dy = 10; m(slope) = dy/dx => 5/3

Slope is more than 1 so we will follow the following method. 

S-3:  Calculate d = 2*dx-dy , so d=2*3 – 5 = 1. 

S-4: Always remember the rule for any line algorithm, If m is less than 1 then always increment x and calculate y. If m is more than 1 then do opposite , which is, always increment y and calculate x.

In this case, we will increase y by 1 every step as m (Slope) is more than 1 and calculate y as follows.

a) If d >=0 then x1 = x1 + 1 and y1 = y1 +1 with  new d = d + 2*(dx-dy)

b) If d<0 then x1 = x1 (remains same) and y1=y1+1  with new d = d + 2*dx

Note: y is always increasing ->Why?, its becasue for m>1, always increase y.
S.No.X1Y1dPixel Plotted
123d = 2*dx – dy = 12,3
234From step 4 (a) d =1+ 2*( 3-5) = -33,4
335From step 4 (b) d =-3+ 2* 3= 33,5
446From step 4 (a) d=3+ 2*(3-5)=-14,6
547From step 4 (b) d = -1 + 2*3 = 54,7
658From step 4 (a) d = 5+ 2*(3-5) = 15,8
 Cant increase x as x has reached finalCant increase y as y has reached final So algo will stop here. 

Draw a line from (1,1) to (8,7) using Bresenham’s Line Algorithm.

Case - When Slope (m) <1

Now let’s solve the same numerical using BLA Algorithm.

S-1:  x1=1; y1=1; x2=8; y2=7.

S-2: dy=7-1 = 6 and dx = 8-1 = 7

 dy-dx = 6-7 = -1; and 2 * dy = 12;

S-3: Calculate d = 2*dy-dx , so d=2*6 – 7 = 5 (Note the change here for m<1)

S-4: We will increase x by 1 every step as m is less than 1 and calculate y as follows 

Rule: If slope (m) is less than 1 (m<1) then always increase x and calculate y.

a) If d >=0 then x1 = x1 + 1 and y1 = y1 + 1 with new d = d + 2*(dy-dx)

b) If d<0 then x1 = x1 + 1 and y1 will not change with new d = d + 2*dy

S.No.X1Y1dPixel Plotted
111d = 2*dy – dx = 51,1
222From step 4 (a) d = 5 + 2*(6-7)= 32,2
333From step 4 (a) d = 3 +2*(6-7) = 13,3
444From step 4 (a) d = 1 + 2*(6-7) = -14,4
554From step 4 (b) d = -1 + 2*6 = 115,4
665From step 4 (a) d = 11 +2(6-7) = 96,5
7 76 d=9+2*(6-7)=7 7,6
887 Algorithm will stop here after plotting final pixel(8,7).(8,7)

We have disabled - Right- Click - How about stay to read :)