Showing posts with label algorithm. Show all posts
Showing posts with label algorithm. Show all posts

Tuesday, 23 February 2016

Bresenham's circle drawing


Q)Circle generation using bresenham’s circle drawing algorithm.
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#include <math.h>
#include<iostream.h>
void drawcir(float x,float y,float p,float q){
putpixel(x+p,y+q,3);
putpixel(x-p,y+q,3);
putpixel(x+p,y-q,3);
putpixel(x-p,y-q,3);
putpixel(x+q,y+p,3);
putpixel(x-q,y+p,3);
putpixel(x+q,y-p,3);
putpixel(x-q,y-p,3);
    }
int main()
{
   int gdriver = DETECT, gmode;
   initgraph(&gdriver, &gmode, "C:\\TC\\bgi");
   float gx,gy,grad;
   cout<<"enter the x coordinateof the centre";
   cin>>gx;
   cout<<"enter the y coor of the centre";
   cin>>gy;
  cout<<"enter the radious of the circle";
  cin>>grad;
  float p=0;
  float q=grad;
  float d=3-2*grad;
  while(p<q){
  drawcir(gx,gy,p,q);
p++;
if(d<0)
{d=d+4*p+6;
}
  else{
q--;
  d=d+4*(p-q)+10;
  }
drawcir(gx,gy,p,q);
  }
   getch();
   closegraph();
   return 0;
}

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;
}