Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
Bresenham’s Line Algorithm  Bresenham’s algorithm is a highly efficient incremental method for scan converting lines.Worki...
The line is best approximated by those pixelsthat fall the least distance from its true pathbetween P1’ and P2’.The coordi...
P2’            Tyi+1    t                s  yi    P1’ S       xi   xi             +1
(b)   If T is chosen, we have           xi+1 = xi + 1          yi+1 = yi + 1The actual y coordinate of line at x = xi+1 is...
Now consider the difference between these twodistance values s – t.When s-t < 0 we have s < t & the closest pixel is S.Whe...
From (1)  y = m(xi + 1) + bs–t =    2( m(xi + 1) + b ) – 2yi – 1     = 2m(xi + 1) + 2b – 2yi –1         _____(B)Put m = ∆...
Take decision variable di = ∆x (s – t)    ______(A)di = 2∆y(xi + 1) + ( 2b – 2yi –1 ) ∆x  = 2∆yxi + 2∆y + ( 2b – 2yi –1 ) ...
Subtract (2) from (3), we getdi+1 – di =2∆y xi+1 – 2∆x yi+1 +C - 2∆yxi + 2∆x yi - CPut    xi+1 = xi +1di+1 – di = 2∆y (xi ...
Then, yi+1 = yi + 1 put in (4)di+1 = di + 2∆y - 2∆x (yi + 1 - yi)       = di + 2∆y - 2∆x       = di + 2(∆y - ∆x) Now if ch...
Thus we have, di+1 = di + 2(∆y - ∆x)     if di > 0         di + 2∆y            if di < 0Now we calculate d1 from the origi...
But y = mx + b     mx + b – y = 0     mx1 + b – y1 = 0Thus d1 = ∆x[2(0) + 2m –1]d1 = ∆x[ 2m – 1]   = ∆x [ 2∆y –1 ]      ...
Algorithm:Algorithm for scan converting a line fromP1’(x1’,y1’) to P2’(x2’,y2’) with x1’<x2’ &      0<m<1.(i) int x = x1’ ...
(iv) Setpixel (x,y)(v) while (x < x2’)    { x++       if ( d < 0)       d = d + dS   else    { y++       d = d + dT     } ...
Example 1 Scan convert a line from (1,1)  &(8,5) with 0<m<1.   x1’= 1, y1’= 1, x2’ = 8, y2’ = 5 x= y= dx=       dy=     dT...
Upcoming SlideShare
Loading in …5
×

Bresenham's line algo.

6,604 views

Published on

  • Be the first to comment

Bresenham's line algo.

  1. 1. Bresenham’s Line Algorithm Bresenham’s algorithm is a highly efficient incremental method for scan converting lines.Working: Let we want to scan convert a line with slope 0<m<1. We start with pixel P1’(x1’,y1’) then select subsequent pixels in the horizontal direction towards P2’(x2’,y2’).Once a pixel is chosen at any step, the next pixel is either the one to its right or the one to its right & up due to the limit on m.
  2. 2. The line is best approximated by those pixelsthat fall the least distance from its true pathbetween P1’ and P2’.The coordinates of the last chosen pixel upon entering step i are (xi,yi). The task is to choose the next one between the bottom pixel S & top pixel T.(a) If S is chosen, we have xi+1 = xi +1 yi+1 = yi
  3. 3. P2’ Tyi+1 t s yi P1’ S xi xi +1
  4. 4. (b) If T is chosen, we have xi+1 = xi + 1 yi+1 = yi + 1The actual y coordinate of line at x = xi+1 is y = mx +b = m xi+1 + b = m(xi + 1) + b ______(1)The distance from S to actual line in y direction is s = y – yi.The distance from T to actual line in y direction is t = (yi+1) – y
  5. 5. Now consider the difference between these twodistance values s – t.When s-t < 0 we have s < t & the closest pixel is S.When s-t > 0 we have s > t & the closest pixel is T.We also choose T when s – t = 0.The difference is:-s – t = ( y – yi ) – [(yi+1) – y] = y – yi – yi – 1 + y = 2y – 2yi – 1
  6. 6. From (1)  y = m(xi + 1) + bs–t = 2( m(xi + 1) + b ) – 2yi – 1 = 2m(xi + 1) + 2b – 2yi –1 _____(B)Put m = ∆y / ∆xs – t = 2∆y (xi + 1) + 2b – 2yi –1 ∆x∆x (s – t) = 2∆y (xi + 1) + ( 2b – 2yi –1 ) ∆x
  7. 7. Take decision variable di = ∆x (s – t) ______(A)di = 2∆y(xi + 1) + ( 2b – 2yi –1 ) ∆x = 2∆yxi + 2∆y + ( 2b – 2yi –1 ) ∆x = 2∆yxi + 2∆y + 2b∆x – 2yi∆x – ∆x = 2∆yxi – 2yi∆x + 2∆y + 2b∆x – ∆x = 2∆yxi – 2yi∆x + 2∆y + (2b– 1) ∆x = 2∆yxi – 2∆x yi + C _____(2)where, C = 2∆y + (2b– 1) ∆xSimilarly, di+1 = 2∆y xi+1 – 2∆x yi+1 + C _____(3)
  8. 8. Subtract (2) from (3), we getdi+1 – di =2∆y xi+1 – 2∆x yi+1 +C - 2∆yxi + 2∆x yi - CPut xi+1 = xi +1di+1 – di = 2∆y (xi +1) – 2∆x yi+1 - 2∆yxi + 2∆x yi = 2∆y (xi +1) – 2∆x yi+1 - 2∆yxi + 2∆x yi = 2∆y (xi +1) - 2∆yxi – 2∆x yi+1 + 2∆x yi = 2∆y (xi +1 - xi ) – 2∆x (yi+1 – yi ) di+1 = di + 2∆y - 2∆x (yi+1 – yi ) ________(4)Now if choose pixel T, it means that s – t > 0
  9. 9. Then, yi+1 = yi + 1 put in (4)di+1 = di + 2∆y - 2∆x (yi + 1 - yi) = di + 2∆y - 2∆x = di + 2(∆y - ∆x) Now if choose pixel S, it means that s - t < 0 di < 0 as di = (s – t)∆xthen, yi+1 = yi put in (4)di+1 = di + 2∆y - 2∆x (yi - yi) = di + 2∆y
  10. 10. Thus we have, di+1 = di + 2(∆y - ∆x) if di > 0 di + 2∆y if di < 0Now we calculate d1 from the original valueof diFrom (A)  di = ∆x(s – t)From (B)  = ∆x(2m(xi + 1) + 2b – 2yi –1 ) d1 = ∆x(2m(x1 + 1) + 2b – 2y1 –1 ) = ∆x[2(mx1 + b - y1) + 2m –1 )
  11. 11. But y = mx + b  mx + b – y = 0  mx1 + b – y1 = 0Thus d1 = ∆x[2(0) + 2m –1]d1 = ∆x[ 2m – 1] = ∆x [ 2∆y –1 ] ∆xd1 = 2∆y – ∆x
  12. 12. Algorithm:Algorithm for scan converting a line fromP1’(x1’,y1’) to P2’(x2’,y2’) with x1’<x2’ & 0<m<1.(i) int x = x1’ and y = y1’(ii) int dx = x2’ – x1’ dy = y2’ – y1’ dT = 2(dy – dx) dS = 2dy(iii) int d = 2dy – dx
  13. 13. (iv) Setpixel (x,y)(v) while (x < x2’) { x++ if ( d < 0) d = d + dS else { y++ d = d + dT } Setpixel (x,y) }
  14. 14. Example 1 Scan convert a line from (1,1) &(8,5) with 0<m<1. x1’= 1, y1’= 1, x2’ = 8, y2’ = 5 x= y= dx= dy= dT= dS= d= Plot x1’ y1’ x2’-x1’ y2’- y1’ 2(dy-dx) 2dy 2dy-dx 1 1 7 4 -6 8 1 1,1 2 2 -5 2,2 3 3 3,2 4 3 -3 4,3 5 5 5,3 6 4 -1 6,4

×