Implementation of Bresenham's circle drawing algorithm [CS1255 - Graphics and Multimedia Lab]

AIM:
           To write a "C++" program for the implementation of Bresenham's circle drawing algorithm in CS1255 - Graphics and Multimedia Lab.


SOURCE CODE:

#include<iostream.h>
#include<graphics.h>
#include<conio.h>
#include<math.h>
#include<stdio.h>
class MyCircle
{
 int x,y,r,d,x1,y1;
 public:
    MyCircle();
   void ShowCircle();
};
MyCircle::MyCircle()
{
 x=0;
 y=0;
 cout<<"\nEnter the coordinates of the circle : ";
 cin>>x>>y;
 cout<<"\nEnter the radius of the circle : ";
 cin>>r;
}
void MyCircle::ShowCircle()
{
 char *s;
 x1=0;
 y1=r;
 d=3-2*r;
 while(x1<y1)
  {
   putpixel((x+x1+320),(-y+y1+240),5);
   putpixel((x-x1+320),(-y+y1+240),5);
   putpixel((x+x1+320),(-y-y1+240),5);
   putpixel((x-x1+320),(-y-y1+240),5);
   putpixel((x+y1+320),(-y+x1+240),5);
   putpixel((x-y1+320),(-y+x1+240),5);
   putpixel((x+y1+320),(-y-x1+240),5);
   putpixel((x-y1+320),(-y-x1+240),5);
   if(d<0)
    d+=4*x1+6;
   else
    {
     d+=4*(x1-y1)+10;
     y1--;
    }
    x1++;
  }
  setcolor(5);
  outtextxy(318+x,235+y,"");
  setcolor(90);
  sprintf(s,"Center(%d,%d)",x,y);
  outtextxy(20,10,"The center is at");
  outtextxy(20,20,s);
  sprintf(s,"Radius=%d",r);
  outtextxy(20,30,s);
  outtextxy(218,10,"BRESENHAM'S CIRCLE ALGORITHM");
  getch();
}
void main()
{
 int gd=DETECT,gm,i,j;
 clrscr();
 MyCircle c;
 initgraph(&gd,&gm,"C:/TC/BGI");
 cleardevice();
 rectangle(120,40,320,240);
 rectangle(320,40,520,240);
 rectangle(120,240,320,440);
 rectangle(320,240,520,440);
 for(i=130;i<=520;i+=10)
  for(j=50;j<=430;j+=10)
   putpixel(i,j,75);
 for(i=128;i<=510;i+=10)
  {
   if(i==318)
     continue;
   outtextxy(i,237,"+");
  }
 for(i=47;i<=430;i+=10)
  {
   if(i==237)
     continue;
   outtextxy(317,i,"-");
  }
   outtextxy(310,230,"0");
   outtextxy(530,240,"x");
   outtextxy(320,450,"-y");
   outtextxy(100,240,"-x");
   outtextxy(320,30,"y");
   c.ShowCircle();
   closegraph();
}

OUTPUT:


Previous
Next Post »

If you still didn't find what you're looking for, you can search this website below: