Defining a circle using Polar Co-ordinates :
The second method of defining a circle makes use of polar coordinates as shown in fig:
x=r cos θ y = r sin θ
Where θ=current angle
r = circle radius
x = x coordinate
y = y coordinate
By this method, θ is stepped from 0 to & each value of x & y is calculated.
Algorithm:
Step1: Set the initial variables:
r = circle radius
(h, k) = coordinates of the circle center
i = step size
θ_end=
θ=0
Step2: If θ>θendthen stop.
Step3: Compute
x = r * cos θ y=r*sin?θ
Step4: Plot the eight points, found by symmetry i.e., the center (h, k), at the current (x, y) coordinates.
Plot (x + h, y +k) Plot (-x + h, -y + k)
Plot (y + h, x + k) Plot (-y + h, -x + k)
Plot (-y + h, x + k) Plot (y + h, -x + k)
Plot (-x + h, y + k) Plot (x + h, -y + k)
Step5: Increment θ=θ+i
Step6: Go to step (ii).
Program to draw a circle using Polar Coordinates:
#include <graphics.h>
#include <stdlib.h>
#define color 10
void eightWaySymmetricPlot(int xc,int yc,int x,int y)
{
putpixel(x+xc,y+yc,color);
putpixel(x+xc,-y+yc,color);
putpixel(-x+xc,-y+yc,color);
putpixel(-x+xc,y+yc,color);
putpixel(y+xc,x+yc,color);
putpixel(y+xc,-x+yc,color);
putpixel(-y+xc,-x+yc,color);
putpixel(-y+xc,x+yc,color);
}
void PolarCircle(int xc,int yc,int r)
{
int x,y,d;
x=0;
y=r;
d=3-2*r;
eightWaySymmetricPlot(xc,yc,x,y);
while(x<=y)
{
if(d<=0)
{
d=d+4*x+6;
}
else
{
d=d+4*x-4*y+10;
y=y-1;
}
x=x+1;
eightWaySymmetricPlot(xc,yc,x,y);
}
}
int main(void)
{
int gdriver = DETECT, gmode, errorcode;
int xc,yc,r;
initgraph(&gdriver, &gmode, "c:\\turboc3\\bgi");
errorcode = graphresult();
if (errorcode != grOk)
{
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1);
}
printf("Enter the values of xc and yc ,that is center points of circle : ");
scanf("%d%d",&xc,&yc);
printf("Enter the radius of circle : ");
scanf("%d",&r);
PolarCircle(xc,yc,r);
getch();
closegraph();
return 0;
}
Output:
0 Comments