DDA Circle Drawing Algorithm
Turbo C++
#include<iostream.h>
#include<graphics.h>
#include<math.h>
#include<conio.h>
void EightSymmetry(int x,int y,int h,int k)
{
putpixel(x+h,y+k,WHITE);
putpixel(y+h,x+k,WHITE);
putpixel(-x+h,y+k,WHITE);
putpixel(y+h,-x+k,WHITE);
putpixel(x+h,-y+k,WHITE);
putpixel(-y+h,x+k,WHITE);
putpixel(-x+h,-y+k,WHITE);
putpixel(-y+h,-x+k,WHITE);
}
void main()
{
int r,n=0,i,h,k, gd=DETECT, gm;
clrscr();
cout<<"Enter the Centre: ";
cin>>h>>k;
cout<<"Enter Radius: ";
cin>>r;
initgraph(&gd,&gm,"C:/TURBOC3/BGI");
for(i=2;;)
{
n++;
if(i>=r)
break;
i=i*2;
}
float e = 1/pow(2,n);
float x=0,y=r;
while(y>=x)
{
EightSymmetry(x,y,h,k);
x = x + (e*y);
y = y - (e*x);
}
getch();
closegraph();
}
Bresenham Circle Drawing Algorithm
Turbo C++
#include<iostream.h>
#include<graphics.h>
#include<conio.h>
void EightSymmetry(int x,int y,int h,int k)
{
putpixel(x+h,y+k,WHITE);
putpixel(y+h,x+k,RED);
putpixel(-x+h,y+k,GREEN);
putpixel(y+h,-x+k,BLUE);
putpixel(x+h,-y+k,YELLOW);
putpixel(-y+h,x+k,CYAN);
putpixel(-x+h,-y+k,MAGENTA);
putpixel(-y+h,-x+k,BROWN);
}
void main()
{
int gd=DETECT,gm,h,k,r,p;
clrscr();
cout<<"Enter the Centre: ";
cin>>h>>k;
cout<<"Enter the Radius: ";
cin>>r;
initgraph(&gd,&gm,"C:/TURBOC3/BGI");
p = 3 - (2*r);
float x = 0 , y = r;
while(x<=y)
{
EightSymmetry(x,y,h,k);
if(p>=0)
{
p = p + 4 * (x-y) + 10;
x++;
y--;
}
else
{
p = p + 4 * x + 6;
x++;
}
}
getch();
closegraph();
}
MidPoint Circle Drawing Algorithm
Turbo C++
#include<iostream.h>
#include<graphics.h>
#include<conio.h>
void EightSymmetry(int x,int y,int xi,int yi)
{
putpixel(x+xi,y+yi,WHITE);
putpixel(y+xi,x+yi,RED);
putpixel(-x+xi,y+yi,GREEN);
putpixel(y+xi,-x+yi,BLUE);
putpixel(x+xi,-y+yi,YELLOW);
putpixel(-y+xi,x+yi,CYAN);
putpixel(-x+xi,-y+yi,MAGENTA);
putpixel(-y+xi,-x+yi,BROWN);
}
void main()
{
int gd=DETECT,gm,h,k,r;
cout<<"Enter the Centre: ";
cin>>h>>k;
cout<<"Enter the Radius: ";
cin>>r;
initgraph(&gd,&gd,"C:/TURBOC3/BGI");
float p = (5/4) - r,x = 0 , y = r;
while(x<=y)
{
EightSymmetry(x,y,h,k);
if(p>=0)
{
p = p + 2 * (x-y) + 5;
x++;
y--;
}
else
{
p = p + 2 * x + 3;
x++;
}
}
getch();
closegraph();
}