Monday, 29 December 2014

Digital Differential Analyzer (DDA) Algorithm

Program to draw a line using DDA Algorithm

#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>

int main()
{
   int x1,x2,dx,p,y1,y2,i=0,dy,xmax,ymax,m;
   float xmid,ymid;
   /* request auto detection */
   int gdriver = DETECT, gmode, errorcode;

   /* initialize graphics mode */
   initgraph(&gdriver, &gmode, "c:\\TurboC3\\BGI");

   /* read result of initialization */
   errorcode = graphresult();

   if (errorcode != grOk)  /* an error occurred */
   {
      printf("Graphics error: %s\n", grapherrormsg(errorcode));
      printf("Press any key to halt:");
      getch();
      exit(1);             /* return with error code */
   }
   line(getmaxx()/2,0,getmaxx()/2,getmaxy());
   line(0,getmaxy()/2,getmaxx(),getmaxy()/2);
   xmax= getmaxx()/2;
   ymax= getmaxy()/2;
   printf("mid point %d \t %d", xmax,ymax);
   printf("\n enter the value of x1 and y1:");
   scanf("%d %d",&x1,&y1);
   printf("\n enter the values of x2 and y2:");
   scanf("%d %d",&x2,&y2);
   if (abs(x2-x1)>abs(y2-y1))
m=abs(x2-x1);
   else
m=abs(y2-y1);
   dx=(x2-x1)/m;
   dy=(y2-y1)/m;
   printf("\n value of dx and dy :%d %d",dx,dy);
   putpixel(x1+319.5,-y1+234.5,2);
   for(i=0;i<m;i++)
   {
x1=x1+dx;
y1=y1+dy;
   putpixel(x1+314.5,-y1+239.5,2);
   }
   getch();
   closegraph();
   return 0;
}
OUTPUT:

No comments:

Post a Comment