// Calculation program for displaying of a asteroid or a flower having n number of corners, Jan. 17 (Sat.), 2009



// Original equation of convex circle: exp(-b*b*x*x)+exp(-c*c*y*y)=2*exp(-a*a), where a*a < sqrt(log(2)), b and c are arbitrary real numbers.

// x=xMAX*sin{(phai-pi/2)*(n/2)} and y=r-r0 are introduced in the above equation for the expression of a polar coordinates into n numbers of corners.

// Furthermore, x=r*sin(phai) and y=r*cos(phai) are used to the expression of a x-y cordinates.



// file name: asteroid.c



#include< stdio.h>

#include< math.h>



void main(void)

{



	double a,b,c,pi;// the value of the pi.

	double r,p,dp;// the radius vector. the phase angle and its minute increment respectively in a polar coodinates

	double r0;// a constant value of the radius vector for an augmentation

	double pmin,pmax;// the minimum and maximum values of the phase angle respectively

	double xmax;// the maximum value of x in original voncave curve. By the way, the minimum value of x (xmin) is that xmin=-xmax.

	int n;// number of corners in the asterroid or the shape of flower

	int i,imax;

	double xx[10001],yy[10001];// Take care of the upper limit of storage memory capacitance.



	FILE *fp;



// Setting of the constants

	pi=3.14159265;

	a=0.83;// [Notation] 0 < a < sqrt(log(2))=0.83255

	b=1;

	c=1;



	xmax=sqrt(-log(2*exp(-a*a)-1)/(b*b));

	r0=0.5*xmax;



	printf("Input the value of n ,where n is natural number. \n n=?\n");// input of numbers of corners n

	scanf("%d",&n);

	printf("n=%d\n",n);



// Setting of the other parameters

	pmin=0;

	pmax=2*pi;

	dp=(pmax-pmin)/10000;// // plotting interval of the phase angle p



// execution of calculation

	i=0;

	for(p=pmin+dp;p< pmax;p=p+dp)

		{

		i++;



		r=r0+(1./c)*sqrt(-log(2.*exp(-a*a)-exp(-b*b*xmax*xmax*sin((p-pi/2.)*(n/2.))*sin((p-pi/2.)*(n/2.)))));



		xx[i]=r*cos(p);

		yy[i]=r*sin(p);



		printf("i=%d,x=%f,y=%f\n",i,xx[i],yy[i]);

		}

	imax=i;



// writing the calculated coordinates data (expressed in (x,y) coordinates) into a textfile named "asteroid.txt"

	fp=fopen("asteroid.txt","w");

	if(fp==NULL)

		{

		printf("FILE OPEN ERROR\n");

		}

	else

		{

		for(i=1;i<=imax;i++)

			{

			fprintf(fp,"%f,%f\n",xx[i],yy[i]);

			}

		fflush(fp);

		fclose(fp);

		}

	printf("end\n");

}// the end of the program





RETURN