Write a Program for Digital Differential Analyzer line drawing algorithm in C

/* Digital Differential Analyzer algorithm    */
#include <graphics.h>
#include <stdio.h>
#include <math.h>
#include <dos.h>
#include <conio.h>

int main( )
{
  float x, y, x1, y1, x2, y2, dx, dy, step;
  int i, gd = DETECT, gm;


  printf("\n\n Enter the value of x1 and y1 :");
  scanf("%d%d",&x1,&y1);

  printf("\n\n Enter the value of x2 and y2:");
  scanf("%d%d",&x2,&y2);

  initgraph(&gd, &gm, "C:\\TURBOC3\\BGI");
  dx = (x2 - x1);
  dy = (y2 - y1);

  if(abs(dx) >= abs(dy))
    step = abs(dx);
  else
    step = abs(dy);
  dx = dx / step;
  dy = dy / step;
  x = x1;
  y = y1;
  i = 1;

  while(i <= step)
  {
    putpixel(x, y, YELLOW);
    x = x + dx;
    y = y + dy;
    i = i + 1;
    delay(100);
  }

  getch();
  closegraph();

  return 0;
}

Previous
Next Post »