DDA Circle Algorithm in C
#include<iostream.h>
#include<conio.h>
#include<dos.h>
#include<math.h>
#include<graphics.h>
void
dda_circle(int xc,int yc,int r);
int main()
{
int xc,yc,r,gd,gm;
clrscr();
cout<<"\nEnter the Center point
(xc,yc) :";
cin>>xc>>yc;
cout<<"\nEnter the Radius
:";
cin>>r;
detectgraph(&gd,&gm);
initgraph(&gd,&gm,"C:\\turboc3\\bgi");
setcolor(10);
line(0,240,640,240);
line(320,0,320,480);
setcolor(5);
xc = 320 + xc;
yc = 240 - yc;
dda_circle(xc,yc,r);
getch();
return 0;
}
void
dda_circle(int xc,int yc,int r)
{
float xc1,xc2,yc1,yc2,eps,sx,sy;
int val,i;
xc1=r;
yc1=0;
sx=xc1;
sy=yc1;
i=0;
do
{
val=pow(2,i);
i++;
}
while(val<r);
eps = 1/pow(2,i-1);
do
{
xc2 = xc1 + yc1*eps;
yc2 = yc1 - eps*xc2;
putpixel(xc+xc2,yc-yc2,3);
xc1=xc2;
yc1=yc2;
}
while((yc1-sy)<eps
|| (sx-xc1)>eps);
}
Output :-
No comments:
Post a Comment