Friday 22 January 2016

DDA algorithm

#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#include <math.h>
#include<iostream.h>
int main(void)
{   int gdriver = DETECT, gmode;
   initgraph(&gdriver, &gmode, "C:\\TC\\bgi");

  float x1,y1,x2,y2,dx,dy,length,xinr,yinr,x,y;
cout<<"enter the x coordinate of the first point";
cin>>x1;
cout<<"enter the y coordinate of the first point ";
cin>>y1;
cout<<"enter the x coordinate of the second point ";
cin>>x2;
cout<<"enter the y coordinate of the second point ";
cin>>y2;
     dx=x2-x1;
     dy=y2-y1;
     if(abs(dy)>abs(dx))
     length=abs(dy);
     else
     length=abs(dx);
       xinr=dx/length;
       yinr=dy/length;
       x=x1;
       y=y1;
for(int i=0 ;i<length;i++)
{   putpixel ( x,y,2);
x+=xinr;
y+=yinr;

}




   getch();
   closegraph();
   return 0;
}

Bresenham algorithm

#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#include <math.h>
#include<iostream.h>
int main(void)
{   int gdriver = DETECT, gmode;
   initgraph(&gdriver, &gmode, "C:\\TC\\bgi");

  float x1,y1,x2,y2,dx,dy,length,xinr,yinr,x,y,e;
cout<<"enter the x coordinate of the first point";
cin>>x1;
cout<<"enter the y coordinate of the first point ";
cin>>y1;
cout<<"enter the x coordinate of the second point ";
cin>>x2;
cout<<"enter the y coordinate of the second point ";
cin>>y2;
     dx=x2-x1;
     dy=y2-y1;
       if(abs(dy)>abs(dx))
     length=abs(dy);
     else
     length=abs(dx);
e=2*dy-dx;
      x=x1;
      y=y1;
      for(int i=0;i<length;i++)
      { while(e>0)
      {
y+=1;
e-= 2*dx;

      }
x+=1;
e=e+2*dy;
    putpixel(x,y,3);
      }

   getch();
   closegraph();
   return 0;
}