Unit-1.2.2 DDA and Direct Line
Unit-1.2.2 DDA and Direct Line
COMPUTING
UNIT-1.1
Bachelor of Computer Science
Computer Graphics
20CAT - 316
3
Direct Line Drawing Algorithm
Perhaps the most natural method of generating a straight line is to use its
equation. First we calculate the slope (m) and the y-intercept (b) using these
equations:
Yend – Ystart
m=
Xend – Xstart
5
Direct Line Drawing Algorithm
6
Direct Line Drawing Algorithm
The second problem concerns lines with a slope whose absolute value is greater than 1
(m > 1). These lines will be near the vertical. Using the direct method, the displayed lines
7
Direct Line Drawing Algorithm
8
Direct Line Drawing Algorithm
x Y = mx + c Round (y) 4
1 1 1 3
2 (5/2)*2-(3/2) = 7/2=3.5 4 2
1
3 (5/2)*3-(3/2) = 12/2 =6 6
0
0 1 2 3 4 5
9
Direct Line Drawing Algorithm
10
Simple DDA Line Drawing Algorithm
To illustrate the idea of DDA algorithm, we still want to draw the line segment
with endpoints (Xstart, Ystart) and (Xend, Yend) having slope :
Yend – Ystart
m=
Xend – Xstart
Any two consecutive points (x1, y1), (x2, y2) lying on this line satisfies the
equation:
y2 – y1
= m Equation 1
x2 – x1
11
Simple DDA Line Drawing Algorithm
continued…
The algorithm is divided into two cases that depend on the absolute value of the
slope of the line. Note that:
We should test the line endpoints to ensure that the line is neither horizontal
(Xstart = Xend) nor vertical (Ystart =Yend). If the line is horizontal use the
horizontal line drawing algorithm and use the vertical line drawing algorithm
when it is vertical.
The starting and ending points of the line are plotted separately since these
values are known from the given data.
In the two cases below the computed incremental values for y2 and x2 may not
be integers. The Round function must be used to obtain an integer coordinate
value.
12
Simple DDA Line Drawing Algorithm
continued…
13
Simple DDA Line Drawing Algorithm
continued…
14
Simple DDA Line Drawing Algorithm
continued…
m = (Yend-Ystart) / (Xend-Xstart)
x y Round(y)
2 0 -
3 y = y + m = 0 + 0.8=0.8 1
4 y = y + m = 0.8 + 0.8=1.6 2
5 y = y + m = 1.6 + 0.8=2.4 2
6 y = y + m = 2.4 + 0.8=3.2 3
7 4 -
16
Example of DDA Line Drawing Algorithm
7
6
5
Fig. 3.15 Pixel display of DDA algorithm
with m < 1
4
0 2 3 4 5 6 7 8 9 17
DDA Line Drawing Algorithm
18
DDA Line Drawing Algorithm Continued..
m = (Yend-Ystart) / (Xend-Xstart)
If (abs(m)>1 and Ystart>Yend) then
Swap endpoints Xstart Xend and Ystart Yend
end if
Set pixel (Xstart, Ystart) with desired color
If abs(m) > 1 then
m = 1/m , y = Ystart + 1
x = Xstart
Next: x= x + m
Set pixel (Round(x), y) with desired color
y=y+1
If y Yend-1 then go to Next
endif
Set pixel (Xend, Yend) with desired color
19
Simple DDA Line Drawing Algorithm
We will use the simple DDA algorithm to draw a line with starting point (2,2) and
ending point (6,7) on a pixel based display. Firstly, we compute the slope m:
m =(Yend–Ystart)/(Xend–Xstart)=(7–2)/(6–2)=5/4
m=1/m = 0.8, y = Ystart + 1 = 2 + 1 = 3 , x= Xstart =2
Table 3.7 – Pixel calculations of DDA line drawing
algorithm with m > 1
y x Round(x)
2 2
3 x = x + m = 2 + 0.8=2.8 3
4 x = x + m = 2.8 + 0.8=3.6 4
5 x = x + m = 3.6 + 0.8=4.4 4
6 x = x + m = 4.4 + 0.8=5.2 5
7 6
20
Example of DDA Line Drawing Algorithm
7
6
5
Fig. 3.16 Pixel display of DDA algorithm
with m > 1
4
0 1 2 3 4 5 6 7 8 21
Algorithm For DDA Line Drawing .
1. Input the two line endpoints and store the left endpoint in (x0,y0)
2. Plot first point (x0,y0)
3. Calculate constants Δx, Δy
4. If |Δx| > |Δy| steps = |Δx| else steps = |Δy|
5. Calculate XInc = |Δx| / steps and YInc = |Δy| / steps
6. At each xk along the line, starting at k=0, Plot the next pixel at (xk + XInc, yk
+ YInc)
7. Repeat step 6 steps times
22
Pseudo Code For DDA Line
Drawing
Void lineDDA(int xa, int ya, int xb, int yb)
{
int dx = xb – xa, dy = yb – ya, steps, k;
float xIncrement, yIncrement, x = xa, y = ya;
if( abs (dx) > abs (dy) ) steps = abs (dx); else steps = abs (dy);
xIncrement = dx / (float) steps; yIncrement = dy / (float) steps; setPixel
(ROUND (x), ROUND (y)); for (k=0; k<steps; k++){
x += xIncrement; y += yIncrement;
setPixel (ROUND(x), ROUND(y));
}
}
23
Advantages and Disadvantages of
DDA Line Drawing
24
References
1. Computer Graphics, 2nd Ed., Hearn & Baker –PHI, New Delhi.
2. Graphics Programming with C By Yashwant Kanetkar, BPB
Publications, New Delhi.
3. Computer Graphics, Schaum’s Outline Series, MGH Publications.
25
THANK YOU
26