![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj9JdvYiSm8D8D8NaX0HHCeyo7m2EmHWmryF77FkQ7B0xtFOERS4T5ZoOIIHyeAvuKnOVuS0Vd0YpfKGZ3HofdoC1MdOGkDd7uGYK-NR2gkZKC4IxWHeEVlCb9gIGbHZL3uWCSjDpEVYbg/s400/prog.png)
DDA Line Drawing Algorithm program in C
In computer graphics, a hardware or software implementation of a digital differential analyzer (DDA) is used for linear interpolation of variables over an interval between start and end point. DDAs are used for rasterization of lines, triangles and polygons. In its simplest implementation the DDA Line drawing algorithm interpolates values in interval [(xstart, ystart), (xend, yend)] by computing for each xi the equations xi = xi−1+1/m, yi = yi−1 + m, where Δx = xend − xstart and Δy = yend − ystart and m = Δy/Δx.
Implementation in C
#include<graphics.h> #include<stdio.h> #include<conio.h> #include<math.h> void main() { int gd=DETECT,gm=DETECT,len,dx,dy,i,x1,y1,x2,y2; float xi,yi,x,y; clrscr(); printf("Enter the starting point x1 & y1\n"); //Enter starting point scanf("%d %d",&x1,&y1); printf("Enter the end point x2 & y2\n"); //Enter End points scanf("%d %d",&x2,&y2); initgraph(&gd, &gm, ""); //Initialize graphics driver cleardevice(); dx=x2-x1; dy=y2-y1; if(abs(dx)>abs(dy)) { len=abs(dx); } else { len=abs(dy); } xi=dx/(float)len; yi=dy/(float)len; x=x1; y=y1; putpixel(x1,y1,4); //Plot points x1 and y1 on screen, 4 is color code. for(i=0;i<len;i++) { x+=xi; y+=yi; putpixel(x,y,4); } getch(); }