7/30/2019 1.11 Bresenham's Algorithm
1/19
Advantage & Disadvantage of
DDA algo
7/30/2019 1.11 Bresenham's Algorithm
2/19
Adva
It is a faster method for calculating pixel positions than
the direct use.
Disadv
The accumulation of round off error in successiveadditions of the floating point increment, can cause the
calculated pixel positions to drift away from the true line
path for long line segments. Rounding operations and floating point procedure in
lineDDA are still time consuming.
7/30/2019 1.11 Bresenham's Algorithm
3/19
Bresenhams Algorithm
TheoryFirst consider scan conversion process for lines
with +ve slopes
7/30/2019 1.11 Bresenham's Algorithm
4/19
Starting from the left end point(x0,y0) of a
given line, we step to each successive
column (x position) and plot the pixel
whose scan line y value is closest to the
line path.
7/30/2019 1.11 Bresenham's Algorithm
5/19
Bresenham's line algorithm
d1
d2
x x+1
y
y = m(x+1) + b
y = mx + b
7/30/2019 1.11 Bresenham's Algorithm
6/19
Assume the given endpoints are (xa,ya)
and (xb,yb), that xa < xb.
Define x = xb - xa; y = yb - ya;
As in the DDA algorithm we will iteratethrough the relevant columns and set one
pixel in each column.
7/30/2019 1.11 Bresenham's Algorithm
7/19
Assume that in the k-th iteration, the pixel
(xk,yk) is set.
Then in the next iteration xk+1,,, ,We will
select either (xk+1,yk) or (xk+1,yk+1)
7/30/2019 1.11 Bresenham's Algorithm
8/19
The y coordinate on the mathematical line at pixel
position xk+1 is calculated as
Y = m(Xk+1) + b
d1=y yk
= m(xk+1)+b - yk
d2=(yk+1)-y
=yk+1 m(xk+1)-b
7/30/2019 1.11 Bresenham's Algorithm
9/19
7/30/2019 1.11 Bresenham's Algorithm
10/19
But xk+1=xk+1
Pk+1 PK
= 2 yxk + 2 y 2 x(yk+1) + c
-[ 2 yxk - 2 xyk +c
2 y - 2 x(yk+1 yk)
7/30/2019 1.11 Bresenham's Algorithm
11/19
7/30/2019 1.11 Bresenham's Algorithm
12/19
7/30/2019 1.11 Bresenham's Algorithm
13/19
Bresenham's line algorithm
(slope 1)
input line endpoints, (x0,y0) and (xn, yn)
calculate x = xn - x0 and y = yn - y0 calculate parameter p0 = 2 y - x
set pixel at position (x0,y0) repeat the following steps until (xn, yn) is reached:
if pi < 0
set the next pixel at position (xi +1, yi )
calculate new pi+1 = pi + 2 y if pi 0
set the next pixel at position (xi +1, yi + 1 )
calculate new pi+1 = pi + 2(y - x)
7/30/2019 1.11 Bresenham's Algorithm
14/19
Compute using Bresenhams
End points (20,10) and (30,18) Slope= 0.8
x=10 y=8
Initial decision parameter has the value
P0=2 y - x
=6
Successive decision parameters
increments r
2 y = 16 , 2 y 2 x = - 4
7/30/2019 1.11 Bresenham's Algorithm
15/19
We plot initial point (20 , 10) anddetermine successive pixel position along
the line path from the decision parameters
as
7/30/2019 1.11 Bresenham's Algorithm
16/19
K Pk (xk+1,yk+1) K Pk (xk+1,yk+1)
0 6 (21,11) 6 2 (27,16)
1 2 (22,12) 7 -2 (28,16)2 -2 (23,12) 8 14 (29,17)
3 14 (24,13) 9 10 (30,18)
4 10 (25,14)
5 6 (26,15)
7/30/2019 1.11 Bresenham's Algorithm
17/19
DDA versus Bresenhams
Algorithm DDA works with floating point arithmetic Rounding to integers necessary
Bresenhams algorithm uses integer
arithmetic
Constants need to be computed only once
Bresenhams algorithm generally faster
than DDA
7/30/2019 1.11 Bresenham's Algorithm
18/19
Bresenhams Algorithm Code
void lineBres(int xa, int ya, int xb, int yb)
{
int dx = abs(xb xa); int dy =abs( yb ya);
Int p=2*dy dx;
int twoDy = 2*dy;
int twoDyDx=2*(dy dx);
Int x, y,xend;
If(xa>xb)
{
x = xb;
y = yb;
xEnd=xa;
}
7/30/2019 1.11 Bresenham's Algorithm
19/19
else{
x = xa;
y = ya;
xEnd=xb;}
setPixel(x,y);
While (x < xEnd){
x++;
If (p