Articles

Explain DDA Line Drawing Algorithm in Computer Graphics with Example

Share

Explain DDA Algorithm?

Digital differential Analyzer is a line drawing algorithm which calculates and plots coordinates on the basis of the previously calculated intermediate points until it reaches to the final point.  However, this algorithm works on the concept of the slope-intercept equation.

DDA Line Drawing Algorithm:-

      1. Since, A line segment has an initial point (x0,y0) and a final point (x1,y1), so get the input from the user regarding the initial and final point.
      2. After getting the input, calculate the value of Δx and Δy.  (Δ represents the difference between two points)   Δx= x1-x0,  Δy= y1-y0
      3. Find the slope of the line by usin.g: Slope =Δy/Δx
      4. if Slope > 1 then do,

          While x isn’t equivalent to the final point (x1)          do,

x0+k = xk + (1/m)

y=y+1

plot(x,y)

          End while

         else

         While y isn’t equivalent to the final point (y1)          do,

y0+k = yk + (m),  x=x+1

plot(x,y)

End While

C program to draw a line using DDA algorithm in computer graphics

Program:

#include<graphics.h>
#include<stdio.h>
#include<conio.h>
void main()
{
float x,y,x1,y1,delx,dely;
float slope;
int gr=DETECT,gm;

initgraph(&gr,&gm,"C:\\TURBOC3\\BGI");
printf("\n please enter the initial points of x, y = ");
scanf("%f %f",&x,&y);
printf("\n enter the final points of x, y = ");
scanf("%f %f",&x1,&y1);
dely= y1-y;
delx=x1-x;
slope=dely/delx;
if(slope>1.0)
{
while(y<=y1){
putpixel(x,y,1);
x=x+(1/slope);
y=y+1.0;
}
}

else{
while(x<=x1){
putpixel(x,y,1);
y=y+slope;
x=x+1.0;
}
}

getch();
}

 

Output:

Alternate way:

Alternate Program to draw a line using DDA algorithm

This post was last modified on September 5, 2020

Sandeep Verma

Published by
Sandeep Verma
Tags: algorithm c graphics DDA line