Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 14

Bresenhams Line Algorithm

Bresenhams 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.

The line is best approximated by those pixels


that fall the least distance from its true path
between 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

P2

yi+1
yi

P1 S
xi

xi+1

(b)

If T is chosen, we have
xi+1 = xi + 1
yi+1 = yi + 1
The 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

Now consider the difference between these two


distance 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

From (1) y = m(xi + 1) + b


st =

2( m(xi + 1) + b ) 2yi 1

= 2m(xi + 1) + 2b 2yi 1

_____(B)

Put m = y / x
s t = 2y (xi + 1) + 2b 2yi 1
x
x (s t) = 2y (xi + 1) + ( 2b 2yi 1 ) x

Take decision variable di = x (s t)


di = 2y(xi + 1) + ( 2b 2yi 1 ) x

______(A)

= 2yxi + 2y + ( 2b 2yi 1 ) x
= 2yxi + 2y + 2bx 2yix x
= 2yxi 2yix + 2y + 2bx x
= 2yxi 2yix + 2y + (2b 1) x
= 2yxi 2x yi + C

_____(2)

where, C = 2y + (2b 1) x
Similarly, di+1 = 2y xi+1 2x yi+1 + C _____(3)

Subtract (2) from (3), we get


di+1 di =2y xi+1 2x yi+1 +C - 2yxi + 2x yi - C
Put

xi+1 = xi +1

di+1 di = 2y (xi +1) 2x yi+1 - 2yxi + 2x yi


= 2y (xi +1) 2x yi+1 - 2yxi + 2x yi
= 2y (xi +1) - 2yxi 2x yi+1 + 2x yi
= 2y (xi +1 - xi ) 2x (yi+1 yi )
di+1 = di + 2y - 2x (yi+1 yi )

________(4)

Now if choose pixel T, it means that s t > 0


di > 0 as di = (s t) x

Then, yi+1 = yi + 1 put in (4)


di+1 = di + 2y - 2x (yi + 1 - yi)
= di + 2y - 2x
= di + 2(y - x)
Now if choose pixel S, it means that s - t < 0
di < 0 as di = (s t)x
then, yi+1 = yi
put in (4)
di+1 = di + 2y - 2x (yi - yi)
= di + 2y

Thus we have,
di+1 = di + 2(y - x)
if di > 0
di + 2y
if di < 0
Now we calculate d1 from the original value
of di
From (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 )

But y = mx + b
mx + b y = 0
mx1 + b y1 = 0
Thus d1 = x[2(0) + 2m 1]
d1 = x[ 2m 1]
= x [ 2y 1 ]
x
d1 = 2y x

Algorithm:
Algorithm for scan converting a line from
P1(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

(iv) Setpixel (x,y)


(v) while (x < x2)
{ x++
if ( d < 0)
d = d + dS
else
{ y++
d = d + dT
}
Setpixel (x,y)
}

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=

x1 y1 x2-x1 y2- y1 2(dy-dx) 2dy


1

3
4

5
6

-6

Plot

2dy-dx
1

1,1

-5

2,2

3,2

-3

4,3

5,3

-1

6,4

You might also like