C Program to Draw Line using DDA – Programmerbay https://programmerbay.com A Tech Bay for Tech Savvy Sun, 21 Aug 2022 07:01:38 +0000 en-US hourly 1 https://wordpress.org/?v=6.5.5 https://programmerbay.com/wp-content/uploads/2019/09/cropped-without-transparent-32x32.jpg C Program to Draw Line using DDA – Programmerbay https://programmerbay.com 32 32 C Program to Draw Line using DDA Algorithm in Computer raphics https://programmerbay.com/program-to-draw-a-line-using-dda-algorithm-in-computer-graphics/ https://programmerbay.com/program-to-draw-a-line-using-dda-algorithm-in-computer-graphics/#respond Mon, 04 Jul 2022 07:21:25 +0000 https://www.programmerbay.com/?p=1978 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

]]>
https://programmerbay.com/program-to-draw-a-line-using-dda-algorithm-in-computer-graphics/feed/ 0