C Program to Draw Line using DDA Algorithm in Computer raphics

In this article, we’ll be using DDA algorithm to draw line in C.  DDA line drawing algorithm is the simplest algorithm as compared to others.

Digital differential Analyzer (DDA) 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.

Must Read: DDA Algorithm in computer graphics

C Program to draw a line using DDA algorithm

Program:

#include<graphics.h>
#include<math.h>
#include<conio.h>
void main()
{
int x0,y0,x1,y1,i=0;
float delx,dely,len,x,y;
int gr=DETECT,gm;
initgraph(&gr,&gm,"C:\\TURBOC3\\BGI");
printf("\n****** DDA Line Drawing Algorithm ***********");
printf("\n Please enter the starting coordinate of x, y = ");
scanf("%d %d",&x0,&y0);
printf("\n Enter the final coordinate of x, y = ");
scanf("%d %d",&x1,&y1);
dely=abs(y1-y0);
delx=abs(x1-x0);

if(delx<dely)
{
len = dely;
}
else
{
len=delx;
}
delx=(x1-x0)/len;
dely=(y1-y0)/len;
x=x0+0.5;
y=y0+0.5;
do{
putpixel(x,y,3);
x=x+delx;
y=y+dely;
i++;
delay(30);
}while(i<=len);
getch();
closegraph();
}

 

Output:

dda line drawing algo

Leave a Reply