You are on page 1of 38

Bresenhams line drawing algorithm

Disadvantages of DDA algorithm:


Overload due to many floating
point calculations.

Advantage of bresenhams
algorithm:
Removal of floating point
calculations
Frame buffer

Y
coordinat
es

X
How to decide the

distances?
Error: dist. bet actual line & nearest grid
locantion.

( <= m
1,1
? <= 1)
Error >= 0 , plot (1,1) i.e. increment both x &
y

Error < 0 , plot(1,0) , i.e. increment only x, y


remains same
? (0<= m <1/2 )
(0,0)
1,
0
Algorithm steps
1. Initialize variables
x x1;
y y1;
x abs ( x 2 x1)
y abs ( y 2 y1)

s1 sign ( x 2 x1) //sign function returns


-1 if parameter is <0
s 2 sign ( y 2 y1) 0 if parameter is =0
+1 if parameter is >0
Cont..
2. interchange x and y, depending upon slope
of line

if (y>x)
Temp=x;
x=y;
y=temp;
Interchange=1;
Else
Interchange =0;
End if
Cont..
3. initialize the error term
e=2*y-x
Cont..
4. main loop

For i=1 to x
Setpixel (x, y);
while(e>=0)
if interchange == 1 then
x = x+s1;
else
y = y+s2;
end if
e = e- 2*x;
end while
if interchange == 1then
y=y+s2;
else
x=x+s1;
end if
e=e+2*y;
next i
Draw line from point (0,0) to
(-8,-4)
1.initialize variables.
X=x1=0; y=y1=0;
x=abs(x2-x1)=abs(-8-0)=abs(-8)=8
y=abs(y2-y1)=abs(-4-0)=abs(-4)=4
S1=sign(x2-x1)=sign(-8)=-1
S2=sign(y2-y1)=sign(-4)=-1
2. interchange x and y, depending
upon slope of line
y = 4
If y>x x = 8
Swap y and x; Condition is false
Values are not swapped
Interchange=1; Interchange=0;
Else
Interchange=0
End if
3. initialize error e
e=2*y-x
e=2*(4)-8
=8-8
=0

e=0, initial
error
4. main loop

I Setpixel e X y

1 0 0 0
4. main loop

For i=1 to x // x=8;


Setpixel(x,y)

I Setpixel E X y

1 (0,0) 0 0 0
4. main loop
In this ex.
while(e>=0) Interchange =0
if interchange =1 then S1=-1
x=x+s1; S2=-1
else
y=y+s2;
end if
e=e-2*x;
end while

I Setpixel E X y

1 (0,0) 0 0 0
4. main loop
In this ex.
while(e>=0) Interchange =0
if interchange =1 then S1=-1
x=x+s1; S2=-1
else
y=y+s2;
end if
e=e-2*x;
end while

I Setpixel E X y

1 (0,0) 0 0 0

=0+(-
1)=-1
4. main loop
In this ex.
while(e>=0) Interchange =0
if interchange =1 then S1=-1
x=x+s1; S2=-1
else
y=y+s2;
end if
e=e-2*x;
end while

I Setpixel E X y

1 (0,0) 0 0 0

=0-(2*8) =0+(-
=-16 1)=-1
End loop
4. main loop
In this ex.
if interchange =1then Interchange =0
y=y+s2;
S1=-1
else
x=x+s1; S2=-1
end if
e=e+2*y;
Next i

I Setpixe E X y
l
1 (0,0) 0 0 0
=0- =0+(-
(2*8) 1)=-1
=-16
End
loop
4. main loop
In this ex.
if interchange =1then Interchange =0
y=y+s2;
S1=-1
else
x=x+s1; S2=-1
end if
e=e+2*y;
Next i

I Setpixe E X y
l
1 (0,0) 0 0 0
=0- =0+(-
(2*8) 1)=-1
=-16
End
loop
=0+(-
1)=-1
4. main loop
In this ex.
if interchange =1then Interchange =0
y=y+s2;
S1=-1
else
x=x+s1; S2=-1
end if
e=e+2*y;
Next i

I Setpixe E X y
l
1 (0,0) 0 0 0
=0- =0+(-
(2*8) 1)=-1
=-16
End
loop
=0+(-
1)=-1
=-
16+(2*
8)
4. main loop
In this ex.
if interchange =1then Interchange =0
y=y+s2;
S1=-1
else
x=x+s1; S2=-1
end if
e=e+2*y;
Next i

I Setpixe E X y
l
1 (0,0) 0 0 0
=0- =0+(-
(2*8) 1)=-1
=-16
End
loop
=0+(-
1)=-1
=-
16+(2*
4)
4. main loop

I Setpixel E X y

2 -8 -1 -1
4. main loop

For i=1 to x // x=8;


Setpixel(x,y)

I Setpixel E X y

2 (-1,-1) -8 -1 -1
4. main loop
In this ex.
while(e>=0) Interchange =0
if interchange =1 then S1=-1
x=x+s1; S2=-1
else
y=y+s2;
end if
e=e-2*x;
end while

I Setpixel E X y

2 (-1,-1) -8 -1 -1
4. main loop
In this ex.
while(e>=0) Interchange =0
if interchange =1 then S1=-1
x=x+s1; S2=-1
else
y=y+s2;
end if
e=e-2*x;
end while

I Setpixel E X y

2 (-1,-1) -8 -1 -1

No
change
Loop
ends
4. main loop
In this ex.
if interchange =1then Interchange =0
y=y+s2;
S1=-1
else
x=x+s1; S2=-1
end if
e=e+2*y;
Next i

I Setpixe E X y
l
2 (-1,-1) -8 -1 -1
No
change
End
loop
4. main loop
In this ex.
if interchange =1then Interchange =0
y=y+s2;
S1=-1
else
x=x+s1; S2=-1
end if
e=e+2*y;
Next i

I Setpixe E X y
l
2 (-1,-1) 0 -1 -1
No
change
End
loop
=-1+(-
1)
=-2
4. main loop
In this ex.
if interchange =1then Interchange =0
y=y+s2;
S1=-1
else
x=x+s1; S2=-1
end if
e=e+2*y;
Next i

I Setpixe E X y
l
2 (-1,-1) 0 -1 -1
No
change
End
loop
=-1+(-
1)
=-2
=-
8+(2*4)
=-
4. main loop
In this ex.
if interchange =1then Interchange =0
y=y+s2;
S1=-1
else
x=x+s1; S2=-1
end if
e=e+2*y;
Next i

I Setpixe E X y
l
2 (-1,-1) -8 -1 -1
No
change
End
loop
=-1+(-
1)
=-2
=-
8+(2*4)
=-
4. main loop

I Setpixel E X y

3 0 -2 -1
4. main loop

For i=1 to x // x=8;


Setpixel(x,y)

I Setpixel E X y

3 (-2,-1) 0 -2 -1
4. main loop
In this ex.
while(e>=0) Interchange =0
if interchange =1 then S1=-1
x=x+s1; S2=-1
else
y=y+s2;
end if
e=e-2*x;
end while

I Setpixel E X y
3 (-2,-1) 0 -2 -1
=-1+(-1)
=-2
4. main loop
In this ex.
while(e>=0) Interchange =0
if interchange =1 then S1=-1
x=x+s1; S2=-1
else
y=y+s2;
end if
e=e-2*x;
end while

I Setpixel E X y
3 (-2,-1) 0 -2 -1
=-1+(-1)
=-2
=0-(2*8)
=0-16=-
16
4. main loop
In this ex.
while(e>=0) Interchange =0
if interchange =1 then S1=-1
x=x+s1; S2=-1
else
y=y+s2;
end if
e=e-2*x;
end while

I Setpixel E X y
3 (-2,-1) 0 -2 -1
=-1+(-1)
=-2
=0-(2*8)
=0-16=-
16
Conditio
n false
Loop
4. main loop
if interchange =1then In this ex.
y=y+s2;
Interchange =0
else
x=x+s1; S1=-1
end if S2=-1
e=e+2*y;
Next i

I Setpix E X y
el
3 (-2,-1) 0 -2 -1
=-1+(-
1)=-2
=0-(2*8)
=0-16=-
16
Loop end
4. main loop
if interchange =1then In this ex.
y=y+s2;
Interchange =0
else
x=x+s1; S1=-1
end if S2=-1
e=e+2*y;
Next i

I Setpix E X y
el
3 (-2,-1) 0 -2 -1
=-1+(-
1)=-2
=0-(2*8)
=0-16=-
16
Loop end
-2+(-1)=-
3
4. main loop
if interchange =1then In this ex.
y=y+s2;
Interchange =0
else
x=x+s1; S1=-1
end if S2=-1
e=e+2*y;
Next i

I Setpix E X y
el
3 (-2,-1) 0 -2 -1
=-1+(-
1)=-2
=0-(2*8)
=0-16=-
16
Loop end
-2+(-1)=-
3
=-
4. main loop
if interchange =1then In this ex.
y=y+s2;
Interchange =0
else
x=x+s1; S1=-1
end if S2=-1
e=e+2*y;
Next i

I Setpix E X y
el
3 (-2,-1) 0 -2 -1
=-1+(-
1)=-2
=0-(2*8)
=0-16=-
16
Loop end
-2+(-1)=-
3
=-
Plotting on frame buffer raster
screen
(0,0)
- - - - - - - - -
9 8 7 6 5 4 3 2 1
1. x=0 y=0 -
2. x=-1 y=-1 1
-
3. x=-2 y=-1 2

4. x=-3 y=-2 -
3
5. x=-4 y=-2 -
4
6. x=-5 y=-3 -
7. x=-6 y=-3 5
-
8. x=-7 y=-4 6
-
9. X=-8 y=-4 7
solve
Find intermediate pixels for line
which start at point(3,2) and end at
point (8,6). Plot these pixels on
sample frame
x=3 y=2 buffer.
e=3
e=-7 e=1
x=4 y=3 e=1
e=-9 e=-1
x=5 y=4 e=-1
e=7
(0,
x=6 y=4 e=7 0)
e=-3 e=5
x=7 y=5 e=5

You might also like