0% found this document useful (0 votes)
7 views

CG L7 DDA Line Drawing Algorithm

Uploaded by

Iqra Hanif
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

CG L7 DDA Line Drawing Algorithm

Uploaded by

Iqra Hanif
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 18

Digital Diff erenti al

Analyzer (DDA)
LINE DRAWING
ALGORITHM
WITH SOLVED EXAMPLES
DDA LINE DRAWING ALGORITHM
 The line (vector) generation algorithms which determine the
pixels that should be turned ON are called as digital
differential analyzer (DDA).
 It is one of the technique for obtaining a rasterized straight
line.
 This algorithm can be used to draw the line in all the
quadrants.
DDA ALGORITHM
Assumption:
1. (x1, y1) and (x2, y2) are the two end points of a line and are not equal.
2. Floor integer function is used to convert the real values into integer
value.
e.g.: if value is +8.5 then it is converted to 8 and
if value is -8.5 then it is converted to 9.
3. Sign function is used which returns +1, 0, -1 for the arguments >0, =0,
<0.
e.g.: sign(5) = +1
sign(0) = 0
sign(-1) = -1
sign (1) = 1
CONTD….
Step 1: Approximate the line length
if abs(x2-x1)>= abs(y2-y1) then
length = abs(x2-x1)
else
length = abs(y2-y1)
end if

Step 2: Decide increment


Δx = (x2-x1) / length
Δy = (y2-y1) / length
CONTD….
Step 3: Decide flow (round the values rather than truncate to
handle central pixel addressing correctly)
x = x1 + 0.5 * sign(Δx)
y = y1 + 0.5 * sign(Δy)

Step 4: Begin main loop


i=1
while (i <= length)
setpixel (intger x, integer y)
x = x + Δx
y = y + Δy
i=i+1
end while
finish
Examples
Q. 1 Rasterize the line from (0, 0) to (5,5) using
DDA algorithm.
Solution:
Solution of Q.1 CONTD….
Solution of Q.1 Result :-
CONTD….
The points obtain
to rasterize a line
from (0, 0) to (5, 5)
are :
(0, 0)
(1, 1)
(2, 2)
(3, 3)
(4, 4)
CLO 2

Q2. Rasterize the line from (1, 0) to (5,4)


using DDA algorithm.
Note
• In Question 1, the end points to draw the line
are given directly so, the steps of the algorithm
are followed to implement it.
• But if the end points are not given and instead
the equation is given then first we have to find
the end points of a line from that equation and
then follow the steps of algorithm.
• The procedure is demonstrated in Q.2 by
solving the question asked in university paper.
Q. 2 Rasterize the line for the equation
x/2 + y/10 = 1 using DDA line algorithm.
Solution: Given is x/2 + y/10 = 1 --------eq. (1)
Put x = 0 in eq. 1 , then y = 10
the 1st end point (x1,y1) is (0,10)
Put y = 0 in eq. 1 , then x = 2
the 2nd end point (x2,y2) is (2,0)
Here,
x1=0 , y1=10 , x2=2 , y2=0
Solution of Q.2 CONTD….
Step 1: abs(x2-x1) = abs(2-0) = 2
abs(y2-y1) = abs(0-10) = 10
Here, abs (y2-y1)> abs(x2-x1)
length =10
Step 2: Δx = (x2-x1) / length = (2-0)/10 = 0.2
Δy = (y2-y1) / length = (0-10)/10 = -1
Solution of Q.2 CONTD….
Step 3: x = x1 + 0.5 * sign(Δx)
x = 0 + 0.5 * sign (0.2)
x = 0 + 0.5 * 1
 x = 0.5
y = y1 + 0.5 * sign(Δy)
y = 10 + 0.5 * sign (-1)
y = 10 + 0.5 * -1
 x = 9.5
Solution of Q.2 CONTD….
Step 4: i

1
Set pixel

(0,9)
x

0.5
y

9.5
2 (0,8) 0.7 8.5
3 (0,7) 0.9 7.5
4 (1,6) 1.1 6.5
5 (1,5) 1.3 5.5
6 (1,4) 1.5 4.5
7 (1,3) 1.7 3.5
8 (1,2) 1.9 2.5
9 (2,1) 2.1 1.5
10 (2,0) 2.3 0.5
11 END 2.5 -0.5
Solution of Q.2 Result :-
CONTD…. 10
The points obtain to 9
rasterize a line for
8
the equation
x/2 + y/10 = 1 are : 7

(0,9) 6
(0,8) 5
(0,7) 4
(1,6)
3
(1,5)
(1,4) 2

(1,3) 1
(1,2)
(2,1) 0
1 2 3 4 5 6
(2,0)
Another Task
• Apply DDA algorithm on the
following equation:
• 5x/7 + 18y/7 = 1
Advantages and Disadvantages of DDA
 Advantages :
1. Simplest line drawing algorithm
2. No special skills required for its implementation
3. DDA draws the line faster than drawing the line by directly using the
line equation.
 Disadvantages :
1. It dependents on orientation which makes the end point accuracy
poor.
2. It requires floating point addition to determine each successive point
which is time consuming.
3. Error due to limited precision in floating point representation may
cause calculated points to shift away from their actual position when
the line is relatively long.
THANK YOU

You might also like