Sub: Computer Graphics & Multimedia
DDA LINE DRAWING ALGORITHM WITH ITS EXAMPLE AND PRACTICAL
IMPLEMENTATION
1. DDA means Digital Differential Analyzer.
2. It is one of the line drawing algorithms.
3. Algorithm of DDA:
1.
2.
Read End Points of line (x1,y1) & (x2,y2).
dx = (x2-x1)
dy = (y2-y1)
3. If (dx>=dy) then
length=dx
else
length=dy
end if
4. dx = (x2-x1)/ length
//This makes either dx or dy equal to 1 i.e. Incremental
dy = (y2-y1)/ length
// value of x or y is 1.
5. x= x1+0.5*sign(dx)
.
y= y1+0.5*sign (dy)
6.
// Sign shows Algorithm work in all quadrant
// 0.5 for Round up value in integer function.
i=1
// In this loop points are Plotted.
while (i<=length)
{
Plot (Integer(x), Integer(y)
// Use put pixel.
x=x +dx
y= y + dy
i=i+1
}
7. Stop
4. Example of DDA:
Consider the line from (3, 2) to (4, 7), use DDA line algorithm to rasterize this line.
Evaluate and tabulate all the steps involved.
Solution:
Given data,
Ms. Punam Patil
Page 1
Sub: Computer Graphics & Multimedia
1. Consider,
(x1, y1)= (3, 2)
(x2, y2)= (4,7)
2. To find,
x=x2-x1=4-3=1
y=y2-y1=7-2=5
3. Find out Length by checking condition,
If (dx>=dy) then
length=dx
else
length=dy
end if
Length=y=5
4. x=x/Length =1/5= 0.2
y=y/Length=5/5=1
5. Initial values,
X=x1+0.5*sign (x)
=3+0.5* (+)
X=3.5
Y=y1+0.5*sign (y)
=2+0.5*sign(+)
Y=2.5
6. By loop,
The result in tabulated form as,
Ms. Punam Patil
Page 2
Sub: Computer Graphics & Multimedia
Iteration
Pixel
R(x)
R(y)
Initially
0
(3,2)
3.5
2.5
(3,3)
3.7
3.5
(3,4)
3.9
4.5
(4,5)
4.1
5.5
(4,6)
4.3
6.5
Length=5
(4,7)
4.5
7.5
By pictorial presentation in graph is as shown below,
7
6
5
4
3
2
1
(0, 0)
1
5. Program for DDA line drawing algorithm in C:
#include<stdio.h> //Header files
#include<conio.h>
#include<math.h>
#include<graphics.h>
Ms. Punam Patil
Page 3
Sub: Computer Graphics & Multimedia
int main( )
{
int gd=DETECT,gm;
int x1,x2,y1,y2,dx,dy,steps,k;
float xi,yi,x,y;
clrscr( );
initgraph (&gd,&gm,"c:\\turboc3\\bgi");
//Initialize graphics system
printf("Enter the co-ordinates of the first point \n"); //Read the value of point1
printf("x1= ");
scanf("%d/n",&x1);
printf("y1= ");
scanf("%d/n",&y1);
printf("Enter the co-ordinates of the second point \n"); //Read the value of point2
printf("x2= ");
scanf("%d/n",&x2);
printf("y2= ");
scanf("%d/n",&y2);
dx= x2-x1;
dy= y2-y1;
//find dx & dy values
if (abs(dx) > abs(dy))
steps = abs(dx);
else
steps = abs(dy);
//find value of length
xi=(float)dx/steps;
yi=(float)dy/steps;
x=x1; //Initialize starting point
y=y1;
for(k=0;k<steps;k++)
{
putpixel (x,y,15);
x=x+xi;
Ms. Punam Patil
//starting main loop
Page 4
Sub: Computer Graphics & Multimedia
y=y+yi;
}
getch();
closegraph(); //close the graphics system
return 0;
//return the value
}
6. Output:
Ms. Punam Patil
Page 5