0% found this document useful (0 votes)
46 views26 pages

Unit-1.2.2 DDA and Direct Line

The document discusses algorithms for drawing lines on a computer display, including direct line drawing, simple DDA (Digital Differential Analyzer) line drawing, and Bresenham's line drawing algorithm. It describes issues with the direct method related to computation time and gaps in lines with steep slopes. The simple DDA method improves on this by using incremental calculations at each step based on the previous value to avoid repeated multiplications.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
46 views26 pages

Unit-1.2.2 DDA and Direct Line

The document discusses algorithms for drawing lines on a computer display, including direct line drawing, simple DDA (Digital Differential Analyzer) line drawing, and Bresenham's line drawing algorithm. It describes issues with the direct method related to computation time and gaps in lines with steep slopes. The simple DDA method improves on this by using incremental calculations at each step based on the previous value to avoid repeated multiplications.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 26

UNIVERSITY INSTITUTE OF

COMPUTING
UNIT-1.1
Bachelor of Computer Science
Computer Graphics
20CAT - 316

DISCOVER . LEARN . EMPOWER


1
Syllabus
Unit-I
Introduction: Computer Graphics, Computer Graphics Applications.
Video Display Devices: Refresh Cathode Ray Tube, Raster Scan displays, Random Scan Displays,
Architecture of Raster and Random Scan Monitors, Color CRT-monitors, Color Generating
Techniques (Shadow Mask, Beam Penetration), Direct View Storage Tube, Flat Panel Display,
Graphics monitors and Workstations.
Two dimensional Graphics Primitives: Points and Lines, Point plotting Techniques: Coordinate
system, Increment method, Line drawing algorithm: DDA, Bresenham’s line drawing,
Bresenham‘s circle drawing algorithm: Using polar coordinates, Midpoint circle drawing
algorithms Filled area algorithm: Scan line, Polygon filling algorithms, Boundary filled
algorithms. 2
Arbitrary Lines Drawing Algorithm

Drawing lines with arbitrary slope creates several problems.

 Direct Line Drawing Algorithm

 Simple Digital Differential Analyzer (simple DDA) Line Drawing


Algorithm

 Bresenham ’s Line Drawing Algorithm

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

b = Ystart – m Xstart or b = Yend – m Xend


We then draw the line by incrementing the x value one unit from (Xstart,
Ystart) to (Xend, Yend) and at each step solve for the corresponding y value.
For non-integer y value, we must first determine the nearest integer
coordinate.
4
Direct Line Drawing Algorithm
continued..
The following code can be used to draw a line from (Xstart, Ystart) to (Xend, Yend),
where Xstart  Xend
x = Xstart
y = Ystart
m= (Yend-Ystart)/(Xend-Xstart)
b= Ystart – m Xstart
Next: Set pixel (x, Round(y)) with desired color
x= x + 1
y = mx + b
If x  Xend then go to Next
End

5
Direct Line Drawing Algorithm

Note that: the Round function is used to obtain an integer


coordinate value.
There are two fundamental problems with this method:
The first problem is the computational time required to draw the
line. On many computer systems the operations of multiplication
and division can take from 50 to 200 times longer to perform than
an addition.

6
Direct Line Drawing Algorithm

a division operation used to calculate the slope (m)

 a multiplication operation used to calculate b

 There are (Xend-Xstart+1) multiplication operations used to calculate the y value

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

will have gaps between plotted points.

7
Direct Line Drawing Algorithm

The following example illustrates this problem. We will use the


direct method to draw a line with starting point (1,1) and ending
point (3,6) on a pixel based display.

Yend – Ystart 6–1 5


m= = =
Xend – Xstart 3–1 2

8
Direct Line Drawing Algorithm

Table 3.5 – Pixel calculations of direct line drawing 6


algorithm for line with end points (1, 1) to (3, 6)
5

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

Fig. 3.14 – Pixel display of direct line drawing algorithm


for line with end points (1, 1) to (3, 6)

9
Direct Line Drawing Algorithm

There are several methods of correcting both problems.


 Performing incremental calculations at each step based on the
previous step can eliminate repeated multiplication.
 The second problem can be resolved by incrementing y by one
unit and solving the straight line equation for x when absolute
value of the slope is greater than 1 (abs(m) > 1).

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…

Case 1: For abs(m) < 1 and Xstart < Xend,


we generate the line by incrementing the previous x value one unit until Xend is
reached and then solve for y. if Xstart > Xend, swap the two endpoints. Thus
for these consecutive points:
x2 = x1 + 1 or x2 – x1 = 1
Substituting this difference into equation 1 yields:
(y2 – y1)/1= m or y2 = y1 + m Equation 2

13
Simple DDA Line Drawing Algorithm
continued…

Equation 2 enables us to calculate successive values of y from the previous


value by replacing the repeated multiplication with floating point addition.
This method of obtaining the current value by adding a constant to the
previous value is an example of incremental calculation. Using knowledge
of one point to compute the next is a great time saving technique.
The following code can be used to draw a line from (Xstart, Ystart) to (Xend,
Yend) using simple DDA algorithm (case 1):

14
Simple DDA Line Drawing Algorithm
continued…
m = (Yend-Ystart) / (Xend-Xstart)

If (abs(m)<1 and Xstart>Xend) then


Swap endpoints Xstart  Xend and Ystart  Yend
end if
Set pixel (Xstart, Ystart) with desired color
If abs(m) < 1 then
y = Ystart
x = Xstart + 1
Next: y = y + m
Set pixel (x, Round(y)) with desired color
x=x+1
If x  Xend-1 then go to Next
endif
Set pixel (Xend, Yend) with desired color
15
Example of DDA Line Drawing
Algorithm
We will use the simple DDA algorithm to draw a line with starting point (2,0) and
ending point (7,4) on a pixel based display. Firstly, we compute the slope m:
m =(Yend–Ystart)/(Xend–Xstart)=(4–0)/(7–2)=4/5 = 0.8
y = Ystart = 0 x = Xstart + 1 = 2 + 1 =3
Table 3.6 – Pixel calculations of DDA line drawing
algorithm with m < 1

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

Case 2: For abs(m) > 1 and Ystart < Yend,


we generate the line by reversing the above procedure. Namely, we
increment the y value one unit until Yend is reached and then solve for x. if
Ystart > Yend, swap the two endpoints. For these consecutive points:
y2 = y1 + 1 or y2 – y1 = 1
Substituting this difference into equation 1 yields:
1/(x2 – x1) = m or x2 = x1 + 1/m Equation 3

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

You might also like