C Program to implement DDA Line Drawing Algorithm

#include<graphics.h>
#include<conio.h>
#include<math.h>
#include<dos.h>
void main()
 {
             int gd=DETECT,gmode,x1,y1,x2,y2,dx,dy,steps,k;
             float xincr,yincr,x,y;
             initgraph(&gd,&gmode,"c:\\turboc3\\bgi");
             printf("Enter the value of first co-ordinate values(x1,y1)value\n :");
             scanf("%d %d",&x1,&y1);
             printf("Enter the value of first co-ordinate values(x2,y2) value\n : ");
             scanf("%d %d",&x2,&y2);
             dx=x2-x1;
             dy=y2-y1;
             if(abs(dx)>abs(dy))
                  steps=abs(dx);
             else
                  steps=abs(dy);

             xincr=dx/steps;
             yincr=dy/steps;
             x=x1;
             y=y1;
             putpixel(floor(x+0.5),floor(y+0.5),7);
             for(k=1;k<steps;k++)
             {
                 x=x+xincr;
                 y=y+yincr;
                 putpixel(floor(x+0.5),floor(y+0.5),7);
                 delay(5);
             }
             getch();

 }

OUTPUT:

No comments:

Post a Comment