DDA Line Drawing Algorithm

Digital Differential Analyzer (DDA) Algorithm is the simple line generation algorithm. DDA is hardware or software used for interpolation of variables over an interval between start and end point. DDAs are used for rasterization of lines, triangles and polygons.
Below is a program written in C to draw a simple line using DDA Line Drawing Algorithm.
 Code:

#include
#include
#include
void main()
{
   int gd=DETECT,gm,x,y,x1,y1,x2,y2,dx,dy,i;
   float length,xinc,yinc;
   initgraph(&gd,&gm,"C:\\tc\\bgi");
   cleardevice();
   printf("Enter x1,y1,x2,y2:");
   scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
   dx=x2-x1;
   dy=y2-y1;
   if(abs(dx)>=abs(dy))
     length=abs(dx);
   else
     length=abs(dy);
   xinc=dx/length;
   yinc=dy/length;
   x=x1;
   y=y1;
   putpixel((int)x,(int)y,WHITE);
   i=0;
   while(i<=length)
   {
      x=x+xinc;
      y=y+yinc;
      putpixel((int)x,(int)y,WHITE);
      delay(100);
      i=i+1;
    }
    getch();
    closegraph();
    restorecrtmode();
}


Output:

dda

Line using DDA Line drawing Algorithm

Note: Just copy the files “CGA.bgi” & “EGAVGA.bgi” from bgi folder to bin. So, if you have copied these 2 files then you can skip writing “C:\\tc\\bgi” in initgraph with just “”(2 double quotes).
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s