Friday, December 4, 2009

Circle Drawing Algorithms

#include
#include
#include
#include
# define round(a)((int)(a+0.5))
# define pi 3.142857
void eqcircle(int ,int,float,int);
void parcir(int,int,float,int);
void midcir(int,int,float,int);
void brescir(int,int,float,int);
void plot(int ,int,int,int,int);

void main()
{ int gd,gm;
detectgraph(&gd,&gm);
int xc,yc,ch;float r;
initgraph(&gd,&gm,"..\\bgi");
printf("\n enter the centre");
scanf("%d",&xc);
scanf("%d",&yc);
printf("\n enter the radius");
scanf("%f",&r);
printf("\n menu..\n1>equation\n2>parametric\n3>midpoint\n4>bresenham\n");
printf("\n enter choice");
scanf("%d",&ch);
switch(ch)
{ case 1:
eqcircle(xc,yc,r,2);
break;
case 2: parcir(xc,yc,r,10);
break;
case 3: midcir(xc,yc,r,3);
break;
case 4:
brescir(xc,yc,r,4);

break;
default: printf("\n wrong choice");
}
getch();
}
void eqcircle(int xc,int yc, float r,int col)
{ float x,y;
x=0;y=r;
while(x { plot(round(x),round(y),xc,yc,col);

y=sqrt(r*r-x*x);
x++;
}
}
void parcir(int xc,int yc,float r,int col)
{ int k=0; float x,y;
for(k=0;k<360;k++)
{ x=xc+r*cos(pi*k/180);
y=yc+r*sin(pi*k/180);
putpixel(round(x),round(y),col);
delay(10);
}
}

void midcir(int xc,int yc,float r,int col)
{ float p,x,y;
x=0;y=r; p=(5/4)-r;

while(x { if(p<0)
{p+=2*x+1;
}
else
{ p+=2*(x-y)+1;
y--;
}
x++;
plot(round(x),round(y),xc,yc,col);
}
}


//**************************************

void brescir(int xc,int yc,float r,int col)
{
float Dd,delta,x,y;
x=0;y=r;Dd=2*(1-r);

while(x { if(Dd<0)
{ delta=2*Dd+2*y-1;
if(delta<0)
{ x++;
Dd+=2*x+1;
}
else
{ x++;
y--;
Dd+=2*x-2*y+2;
}
}
if(Dd>0)
{delta=2*x-2*Dd+1;
if(delta<0)
{ y--;
Dd+=-2*y+1;
}
else
{ x++;
y--;
Dd+=2*x-2*y+2;
}
}
else
{ x++;
y--;
Dd+=2*x-2*y+2;
}

plot(round(x),round(y),xc,yc,col);

}//end of while


}//end of fun


void plot(int x,int y,int xc,int yc,int col)
{ putpixel(xc+x,yc+y,col);
putpixel(xc-x,yc+y,col);
putpixel(xc+x,yc-y,col);
putpixel(xc-x,yc-y,col);
putpixel(xc+y,yc+x,col);
putpixel(xc-y,yc+x,col);
putpixel(xc+y,yc-x,col);
putpixel(xc-y,yc-x,col);
}

No comments:

Post a Comment