Sunday, November 25, 2012

DRAW A LINE USING DDA ALGORITHM


DRAW A LINE USING DDA ALGORITHM

#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#include<dos.h>
#define ROUND(a)((int)(a+0.5))
int main(void)
{
   /* request auto detection */
   int gdriver = DETECT, gmode, errorcode;
   int xa,xb,ya,yb;
  // int y=0,x=0,i;
   char msg[25];
   /* initialize graphics and local variables */
   initgraph(&gdriver, &gmode, "");

   /* read result of initialization */
   errorcode = graphresult();
   /* an error occurred */
   if (errorcode != grOk)
  {
       printf("Graphics error: %s\n", grapherrormsg(errorcode));
      printf("Press any key to halt:");
      getch();
      exit(1);
  }
 printf("Enter the value for x1,y1:\n");
 scanf("%d%d",&xa,&ya);
 printf("Enter the value for x2,y2:\n");
 scanf("%d%d",&xb,&yb);
 linedda(xa,ya,xb,yb);
 getch();
 closegraph();
  return 0;
}


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(dy);
else
steps=abs(dy);
xincr=dx/(float)steps;
yincr=dy/(float)steps;
putpixel(ROUND(x),ROUND(y),GREEN);
for(k=0;k<steps;k++)
{
x+=xincr;
y+=yincr;
putpixel(ROUND(x),ROUND(y),GREEN);
}
}







































OUTPUT:-


Enter the value for x1,y1:
100
100
Enter the value for x2,y2:
250
300





0 comments:

Post a Comment