Notes Bresenhams Line Drawing Algorithm
Notes Bresenhams Line Drawing Algorithm
Notes Bresenhams Line Drawing Algorithm
Theory:
The Bresenhams line drawing algorithm has provided a
computationally attractive scan conversion algorithm.
It uses integer addition, subtraction and multiplication by 2, and
computer can perorm integer addition, subtraction and multiplication
very rapidly.
The algorithm always increments either ! coordinate or y coordinate by
one unit value depending on slope o line. The increment value in other
variable is determined by e!amining distance between actual line
location and nearest pi!el. This actor is called decision actor.
In Bresenhams algorithm we "rst consider the scan conversion
process or lines with positive slopes less than one #$entle slope lines%.
Thereore, or plotting pi!els along line path ! coordinate is
incremented by unit value and increment in y coordinate is decided
based on decision actor.
Bresenhams Line Drawing Algorithm Working Principal:
(x2,y
2
&'
()
(x!,y
!
"k#!
&
&'
In above "gure, d) and d2 are the distances between actual line path
# y coordinate % and near pi!els &' and &'().
The decision actor *' decides which pi!el to select either &' or &'()
+here, *' , d! # d2 - d) %
.......... #)%
The e/uation o line is,
y , m! ( b
......... #2%
By- Prof.U.N.Abhonkar,Sandip Polytechnic
$
%
(&,&
"
$
$k#
"k#
'
'
0rom this e/uation we can calculate & coordinate by putting value o 1
coordinate as
! , 1'()
*ut value o ! in e/uation #2%
y , m # 1' ( ) % ( b
......... #2%
0rom "g., d) , # &' ( ) % 3y
d) , #&' ( ) % 3 m # 1' ( )% - b
.... rom e/uation #2%
d2 , y 3 &'
d2 , m # 1' ( )% ( b - &'
.... rom e/uation #2%
4alculate # d2 - d) %
d2 - d) , m # 1' ( )% ( b - &' - # &' ( ) % ( m
# 1' ( )% ( b
d2 - d) , 2m # 1' ( ) % (2b 3 &' - # &' ( ) %
d2 - d) , 2m # 1' ( ) % (2b 3 2&' - )
.......... #5%
Lets substitute m , #dy% 6 #d!% in e/uation #5%
d2 - d) , 2 # dy 6 d! % # 1' ( ) % (2b 3 2&' - )
7ultiply both sides by #d!%,
# d! % # d2 - d) % , 2 # dy % # 1' ( ) % (2b #d!% 3 2&' #d!%
- ) #d!%
# d! % # d2 - d) % , 2 dy 1' ( 2 dy (2b #d!% 3 2 d! &' -
d!
# d! % # d2 - d) % , 2 dy 1' 3 2 d! &' ( 2 dy ( d! # 2b 3
)%
8eplace 2dy ( d! #2b3)% by c,
# d! % # d2 - d) % , 2 dy 1' 3 2 d! &' ( c
.......... #9%
+here, c , 2dy ( d! #2b3)% is constant value and is independent pi!el
at &'
0rom e/uation #)%,
*' , d! # d2 - d) %
*' , 2 dy 1' 3 2 d! &' ( c
.... rom e/uation #9%
*' , 2 dy 1' 3 2 d! &' ( c
.......... #:%
I decision actor *' is negative choose lower pi!el
I decision actor *' is positive choose upper pi!el
4alculate ne!t decision actor,
*'() , 2 dy #1'()% 3 2 d! #&'()% ( c
.......... #;%
<ubtract e/uation #:% rom e/uation #;%,
By- Prof.U.N.Abhonkar,Sandip Polytechnic
*'() 3 *' , 2 dy #1'()% 3 2 d! #&'()% ( c 3 2 dy
1' ( 2 d! &' - c
*'() 3 *' , 2 dy #1'()% 3 2 d! #&'()% 3 2 dy 1' (
2 d! &'
*'() 3 *' , 2 dy #1'() - 1' % 3 2 d! #&'() 3 &'%
But 1 coordinate is incremented by ) i.e. # 1'() - 1' % , )
*'() 3 *' , 2 dy 3 2 d! #&'() 3 &'%
*'() , *' ( 2 dy 3 2 d! #&'() 3 &'%
.......... #=%
+here, #&'() 3 &'% is either > or ) depending on sign o *'
I *' ? > then #&'() 3 &'% , >
@/uation #=% becomes,
*'() , *' ( 2 dy
.......... #A%
And i *' B ) then #&'() 3 &'% , )
@/uation #=% becomes,
*'() , *' ( 2 dy - 2 d!
.......... #)>%
<tarting decision parameter *' at ' , > i.e. *> is,
*> , 2 dy #>% 3 2 d! #>% ( c
*> , c
*> , 2dy ( d! #2b3)%
*> , 2dy ( 2 d! b 3d!
*> , 2dy ( 2 d! #y 3 m!% 3d!
*> , 2dy ( 2 d! #> - m#>%% 3d!
*> , 2 dy - d!
.......... #))%
Bresenhams Line Drawing Algorithm:
Step 1: Read line end points (1 ,y1! and (",y"! s#ch that they
are not e$#al
( if they are e$#al plot the point and end the al%orith&!
Step ": 'alc#late,
d ( )" * 1)
dy ( )y" * y1)
Step +: ,nitiali-e startin% point of line i.e. ( 1 and y ( y1
Step .: Plot /rst point i.e. ( , y !
Step 0: 1btained initial 2al#e of decision para&eter Pk as
Pk ( "dy - d
Step 3: if Pk 4 5
6
( 7 1
y ( y
By- Prof.U.N.Abhonkar,Sandip Polytechnic
Pk ( Pk 7 "dy
8
if Pk 9 ( 5
6
( 7 1
y ( y 7 1
Pk ( Pk 7 "dy * "d
8
p#tpiel(,y,:;,<=!
Step >: Repeat step (3! d ti&es
Step >: Stop
A'(antages o) Bresenhams Line Drawing Algorithm:
A ast incremental algorithm
Cses only integer calculations such as addition 6 subtraction and bit
shiting
Accurate, eDcient
Integer calculations
Cses symmetry
Adapted to display circles, ellipse and curves
Disa'(antages o) Bresenhams Line Drawing Algorithm:
4annot generaliEe to arbitrary conics
$ives an optimal "t or lines
7eant or basic line drawing
4annot draw smooth lines
* co'e )or Bresenhams Line Drawing Algorithm:
?? Bresenha&@s Aine BraCin% Al%orith&
D incl#de4stdio.h9
Dincl#de4conio.h9
By- Prof.U.N.Abhonkar,Sandip Polytechnic
Dincl#de4%raphics.h9
2oid &ain(!
6
int %d(B=<='<,%&E
int d,dy,1,y1,",y",,y,pE
clrscr(!E
init%raph (F%d,F%&,G':HH<'HHBI,G!E
printf(G=nter the co-ordinates of the /rst point HnG!E
printf(G1( G!E
scanf(GJd G,F1!E
printf(Gy1( G!E
scanf(GJd G,Fy1!E
printf(G=nter the co-ordinates of the second point HnG!E
printf(G"( G!E
scanf(GJd G,F"!E
printf(Gy"( G!E
scanf(GJd G,Fy"!E
d( "-1E
dy( y"-y1E
p ( " K (dy! * (d!E
( 1E
y ( y1E
p#tpiel (,y,:;,<=!E
for( i ( 1 E i 4 ( d E i77 !
6
,f ( p 4 o!
6
( 7 1 E
P ( P 7 "dy E
8
else
6
( 7 1E
By- Prof.U.N.Abhonkar,Sandip Polytechnic
y ( y 7 1E
P ( P 7 "dy * "dE
8
p#tpiel (,y,:;,<=!E
8
%etch(!E
close%raph(!E
8
By- Prof.U.N.Abhonkar,Sandip Polytechnic