WAP To Draw line using Digital Differential Analyzer Algorithm



#include<stdio.h>
#include<graphics.h>
#include<stdlib.h>
#include<conio.h>
#define ROUND(a) ((int)(a+0.5))
void setpixel(int,int);

void main()
{
       void lineDDA(int, int, int, int);



       int graphdriver;
       int graphmode;
       int x1,y1,x2,y2;
       printf(" Enter coordinates of line: \n ");

       printf("x1= ");
       scanf("%d",&x1);
       printf("\ny1= ");
       scanf("%d",&y1);
       printf("\nx2= ");
       scanf("%d",&x2);
       printf("\ny2= ");
       scanf("%d",&y2);
graphdriver= DETECT;
       initgraph(&graphdriver, &graphmode," \\tc\BGI");

       setpalette(0,4);
       setbkcolor(0);

       lineDDA(x1,y1,x2,y2);

}

void lineDDA(int xa, int ya, int xb, int yb)
{

      int dx=xb-xa, dy=yb-ya, steps, k;
      float xIncr, yIncr, x=xa, y=ya;

      if(abs(dx)>abs(dy))
  steps=abs(dx);
      else
  steps=abs(dy);

      xIncr = dx/(float) steps;
      yIncr = dy/(float) steps;

      putpixel(ROUND(x),ROUND(y),RED);

      for(k=0;k<steps;k++)
      {
   x += xIncr;
   y += yIncr;
   putpixel(ROUND(x),ROUND(y),RED);
      }

      getch();
}


Comments