You are on page 1of 3

Bresenham’s line drawing

algorithm
Chapter 3
 Scan converts lines
Output Primitives using only integer
calculations.
Line drawing algorithms  Sampling at unit x
intervals, we have to
decide which two pixel 13 Y = m.x + b

positions are closer to 12


the actual line path at 11
each step. 10
10 11 12 13
 E.g. (11,11) or (11,12)

Essence of bresenham
 Consider scan conversion
How to do it?
of lines with 0<m<1.
 Look in detail at first
 Test the sign of an integer
parameter, which is the
step:
difference between the We have choice of a
separation of the two pixel move to (1,1) or (1,.2)
positions from the actual path.
2
 Starting pixel is: (xk,yk) yk+3 d2 Exact y-value at x=1 is:
yk+2 Y = m.x + b d1 y = 0.6 + 1 = 1.6
 The next pixel to be drawn will 1
yk+1
be in column xk+1.
yk 0 Since this is closer to 2
 Two possible locations: than 1, we choose (1,2)
xk xk+1 xk+2 xk+3
 (xk+1,yk) OR (xk+1,yk+1) 0 1 2 ie d1 - d2 > 0

1
In general… How to derive the equation?
 At sampling point xk+1, we find d1 & d2. The y-
 In general, suppose we are at (xk,yk) - slope coordinate on the mathematical line at pixel
of line, m, is between 0 and 1
column xk+1 is given as:
 Next move will be to (xk+1, yk) or (xk+1, yk+1)
 y = m(xk + 1) + b

d1 - d2 =
 Then
yk+1
d2 2m(xk + 1) - 2yk + 2b -1  d1 = y – yk = m(xk + 1) + b - yk

yk
d1  And
>0 indicates (xk+1, yk+1)
 d2 = yk+1 – y = yk+ 1 - m(xk + 1) + b
<0 indicates (xk+1, yk)
 The difference is given by:
xk xk+1  d1 – d2 = 2m(xk + 1) – 2yk + 2b -1

 The value for the next decision parameter


can be calculated from the previous
 Let m = ∆y/∆x. Also multiply both sides equation.
by ∆x:  Pk+1 = 2∆y.xk+1 - 2∆x.yk+1 + c
 ∆x(d1 – d2) = 2∆y.xk - 2∆x.yk + c, where  Subtracting pk from pk+1 we get
 c = 2∆y + ∆x(2b – 1)
 pk+1 – pk = 2∆y(xk+1 – xk) - 2∆x(yk+1 – yk)
 Let pk = ∆x(d1 – d2), then  But xk+1 = xk + 1
 pk = 2∆y.xk - 2∆x.yk + c
 pk+1 = pk + 2∆y - 2∆x(yk+1 – yk)
 c is independent of the pixel position, so  The term (yk+1 –yk) is 0 for negative pk
it will be eliminated. and 1 for positive pk
 pk is negative, we plot the lower pixel,  The initial decision parameter is:
else we plot the upper pixel.
 p0 = 2∆y.xk - 2∆x.yk (k = 0; x0 = 0, y0 = 0)
 p0 = 2∆y - 2∆x

2
Algorithm: Summary
 Input two end-points (x0,y0) & (xn,yn). Store the left Example: [Hearn, Baker pg 91]
endpoint (x0,y0).
 Load (x0,y0) in the frame buffer and plot the point.  Two end points (20,10) & (30,18).
 Calculate the initial parameters, i.e. ∆x, ∆y, 2∆y &  ∆x = 10, ∆y = 8
2∆y - 2∆x. Calculate the initial parameter:  Initial parameter: p0 = 2∆y - ∆x = 6
 p0 = 2∆y - 2∆x
 Constant parameters: 2∆y = 16, 2∆y - 2∆x = -4
 Starting from k = 0, repeat the following steps ∆x
k pk (xk+1,yk+) k pk (xk+1,yk+)
number of times
0 6 (21,11) 5 6 (26,15)
1. If pk < 0, then plot (xk+1,yk). The next decision
parameter will be: 1 2 (22,12) 6 2 (27,16)
 pk+1 = pk + 2∆y 2 -2 (23,12) 7 -2 (28,16)
2. If pk > 0, then plot (xk+1, yk+1). The next decision 3 14 (24,13) 8 14 (29,17)
parameter will be: 4 10 (25,14) 9 10 (30,18)
 pk+1 = pk + 2∆y - 2∆x

18
17
16
15
14
13
12
11
10
20 21 22 23 24 25 26 27 28 29 30

You might also like