DDA Line Drawing Algorithm

DDA(Digital DIfferential Analyzer) is a simple line drawing algorithm.It is based on calculating the difference between x and y coordinates of two points and determining which of the two directions need more steps.

/*Header file for built-in graphics functions such as plot() and initgraph()*/
int round(float val)
   return (int)(val+0.5);

/* Function implementing DDA line drawing algorithm */

void line_DDA(float x1, float y1, float x2, float y2)
   int dx=x2-x1, dy=y2-y1, steps, k;
   float xin, yin, X=x1, Y=y1;
   steps = (abs(dx)>abs(dy))?abs(dx):abs(dy);
   xin = dx/(float)steps;
   yin = dy/(float)steps;
   /*This function plots a pixel at coordinates(x,y) specified by first 2 arguments and third argument specifies the color of the pixel*/


/* Main Function */

int main()
  int x1,x2,y1,y2;
  int gd=DETECT, gm;
  initgraph(&gd,&gm,"c:\\tc\\bgi");/*loads the graphic driver and puts syetem into graphics mode*/

 syntax for initgraph() is:
 void initgraph(int *graphdriver, int *graphmode, char *pathtodriver);
 *graphdriver is an integer that specifies the graphics driver to be used.DETECT requests auto-detect for the driver.
 *graphmode is an integer that specifies the initial graphics mode.If *graphdriver equals DETECT,*graphmode is set by initgraph to the highest resolution available for the detected driver.

  printf("\t\t\tDDA LINE DRAWING ALGORITHM\t\t\t\n\tEnter the co-ordinates of first point :");
  scanf("%d %d",&x1,&y1);
  printf("\t\nEnter the coordinates of 2nd point:");
  scanf("%d %d",&x2,&y2);

  //Function call to line drawing algorithm

  /*closegraph function closes the graphics mode, deallocates all memory allocated by graphics system and restores the screen to the original mode*/
  return 0;

DDA Algorithm

You can run this code in Turbo C++. Dev C++ and Microsoft visual studio do not support graphics libraries.
You can download Turbo C++ using this link Turbo C++

Other articles related to Computer Graphics available on TutsHeap are:
Cohen Sutherland Line Clipping Algorithm
Boundary-Fill Algorithm
Flood-Fill Algorithm
Mid-Point Ellipse Drawing Algorithm
Mid-Point Circle Drawing Algorithm
Bresenham’s Line Drawing Algorithm

This content has been helpful to you?

Thanks for contributing!

Yes No