Sei sulla pagina 1di 4

DDA Line Drawing Algorithm

Theory:
The DDA (Digital Diferential Analyzer) method is based on taking unit
steps along one of the coordinates (lets say coordinate) and
compute the corresponding !alues along other coordinate ("
coordinate)#
The unit steps taken are always taken along coordinates of greatest
change# $%ample& if we ha!e change in coordinates d%'(( and
change in " coordinates dy') then we would take unit steps along
coordinate and compute the steps along " coordinate#
DDA Working Principal:
DDA generates a line from the diferential e*uation of line&
y = m x + b
+++++++## slope intercept form
where& m is the slope of line and b is its " intercept
m ' dy , d% ' (%-.%() , (y-.y()
++++++ (%(&y() and (%-&y-)
are two end points of line
d% ' %-.%(
+++++## change in coordinates
dy ' y-.y(
+++++## change in " coordinates
/o for any gi!en inter!al (d%)& we can calculate corresponding "
inter!al (dy) as&
dy ' ( (y-.y() , (%-.%() ) 0 (d%) +++##
increment in coordinate
/imilarly for any gi!en " inter!al (dy)& we can calculate corresponding
inter!al (d%) as&
d% ' ( (y-.y() , (%-.%() ) 0 (dy) ++##
increment in " coordinate
1nce he inter!als are known the !alues for ne%t % and y on straight line
can be obtained as follows&
i2(' i2 d%
"i2(' "i2 dy
x2
(x2,y
2)
By- Prof.U.N.Abhonkar,Sandip Polytechnic
Y
dy
y2
x1

(x1,y1
)
d
%

y1

DDA ine Dra!ing Algori"hm:
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&orithm"
Step #: 'alc$late,
d! ( )!# * !1)
dy ( )y# * y1)
Step +: ,f d!" - ( dy" then
.en&th ( d!
else
.en&th ( dy
/nd if
Step 0: 1increment ( !#-!1" 2 len&th
3increment ( y#-y1" 2 len&th
Step 4: ! ( !15 6.4 7 si&nd!"
y ( y15 6.4 7 si&ndy"
8ere si&n f$nction makes al&orithm 9orks in all %$adrants
ret$rns -1, 6, 1 9hen
Ar&$ment is :6, (6, -6 resp. "
Step ;: i ( 1
9hile i : ( len&th "
<
p$tpi!el ! , y , =8,>/ "
! ( ! 5 1increment
y ( y 5 3increment
i ( i5 1
?
Step @: Stop
A#$an"age% o& DDA ine Dra!ing Algori"hm:
3aster than direct use of line e*uation and it does not do any 4oating
point multiplication
/imple to implement
By- Prof.U.N.Abhonkar,Sandip Polytechnic
'
((),
))
Di%a#$an"age% o& DDA ine Dra!ing Algori"hm:
Time consuming arithmetic because 4oating point addition is still
needed
5recision loss because of rounding of error
5i%els drift farther apart if line is relati!ely large
* co#e &or DDA ine Dra!ing Algori"hm:
22 AAA .ine Ara9in& Al&orithm
Bincl$de:stdio.h-
Bincl$de:conio.h-
Bincl$de:math.h-
Bincl$de:&raphics.h-
Coid main"
<
Coid ddalineint,int,int,int"D
int &d(A/>/'>,&mD
int p1,%1,p#,%#D
clrscr"D
init&raph E&d,E&m,F':GG>'GGBH,F"D
printfF/nter the co-ordinates of the Irst point GnF"D
printfF!1( F"D
scanfFJd F,Ep1"D
printfFy1( F"D
scanfFJd F,E%1"D
printfF/nter the co-ordinates of the second point GnF"D
printfF!#( F"D
scanfFJd F,Ep#"D
printfFy#( F"D
scanfFJd F,E%#"D
ddalinep1,%1,p#,%#"D
&etch"D
By- Prof.U.N.Abhonkar,Sandip Polytechnic
close&raph"D
?
Coid ddalineint !1,int y1,int !#,int y#"
<
int d!,dy,len&th,iD
Koat !inc,yinc,!,yD
d!( !#-!1D
dy( y#-y1D
if absd!" - absdy""
len&th ( absd!"D
else
len&th ( absdy"D
!inc ( Koat" d!2 len&thD
yinc ( Koat" dy2 len&thD
! ( !1D
y ( y1D
i ( 1D
9hile i : len&th"
<
p$tpi!el !,y,B.U/"D
! ( ! 5 !incD
y ( y 5 yincD
i 55 D
?
?
By- Prof.U.N.Abhonkar,Sandip Polytechnic

Potrebbero piacerti anche