// Calculation Program for calculating shape of a flower with the use of a circle (part D), July 4th, 2012



// file name: flower_curve_D.c



#include< stdio.h>

#include< math.h>



void main(void)

{

	double a,pi;// "a" is the radius of original circle of petal and pi=3.1415...

	double b; // coefficient on effect of sharpening the tip of a petal

	double r,z;// "r" and "z"(-π/2 =< z =< π/2)are the moving radius and the phase angle of petal respectively.

	double dz;// increment of "z"

	double f;// the phase angle of petal made of a sharpened original circle

	double p;// phase angle of center circle

	double dp;// increment of "p"

	int n;// numbers of petals

	double alpha;// the sharp angle of the bottom of the petal (The criterion value is "alpha=2*pi/n" in this program.)

	double beta;// angular range to be occupied by a petal around the center circle

	double dbeta;// beta の変分

	double k;// magnification factor of the criterion angle "alpha=2*pi/n" where 0 < k.

	double l;// magnification factor of the angular range "beta" where 0 < l )

	int m;

	int nz;// division numbers of both phases "z" and "beta"

	double zmin,zmax;// the minimum and the maximum values of phase angle "z" respectively

	int i,imax,j;

	double c;// the radius of the center circle

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



	FILE *fp;



// setting of the constants

	pi=3.141592;

	a=1.0;//a=1.0



	b=1.0;//b=1.0





// setting of the other parameters

	printf("Input the numbers of petals. \n n=? ");

	scanf("%d",&n);

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



	alpha=2*pi/n;// the shsarpened angle of the botom of a petal

	beta=alpha;// fundamentally criteria value of "beta" is supposed to be equal to "alpha".





	k=5.3;// k=5.3 //magnification factor of the criterion angle 'alpha=2*pi/n' where 0 < k





	alpha=k*alpha;// the magnificated angle of the botom of the petal





	l=0.3;// l=0.3 // magnification factor of the criterion angular range "beta" where 0 < l





	beta=l*beta;// the magnificated angular range to be occupied by a petal around the center circle





	c=1.5; // c=1.5 // the radius of the center circle



	zmin=-pi/2;

	zmax=pi/2;



	nz=200;



	dz=(zmax-zmin)/nz;// plotting interval of "z"



	dbeta=beta/nz;// plotting interval of "beta"



	dp=2*pi/n/20;// plotting interval of "p"



// execution of calculation

	i=0;



	for(j=1;j<=n;j++) // sweep of n numbers of petals

	{

		m=0;



		for(z=zmin;z<=zmax+dz;z=z+dz) // sweep of phase angle "z" of the each petal

		{

			i++;

			m++;



			r=2*a*cos(z)/(((b+0.1)+fabs(sin(z)))/(b+0.1));

			

			f=-alpha*z/pi+pi/2-2*pi*(j-1)/n;



			xx[i]=r*cos(f)+c*cos(-2*pi*(j-1)/n+pi/2+beta/2-m*dbeta);

			yy[i]=r*sin(f)+c*sin(-2*pi*(j-1)/n+pi/2+beta/2-m*dbeta);



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

		}



		for(p=2*pi*(j-1)/n+beta/2;p<2*pi*j/n-beta/2+dp;p=p+dp)

		{

			i++;



			xx[i]=c*cos(-p+pi/2);

			yy[i]=c*sin(-p+pi/2);



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

		}

	}





	for(p=-beta/2+dp;p<2*pi-beta/2+dp;p=p+dp) // Simply for drawing an additional circle of underlying petal.   By the way, "statement for" may be deleted.

	{

		i++;



		xx[i]=c*cos(-p+pi/2);

		yy[i]=c*sin(-p+pi/2);

	}





	imax=i;



	xx[imax+1]=xx[1];

	yy[imax+1]=yy[1];



// writing the calculated coordinates data of the curve into a textfile named "flower_curve_D.txt"

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

	if(fp==NULL)

		{

		printf("FILE OPEN ERROR\n");

		}

	else

		{

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

			{

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

			}

		fflush(fp);

		fclose(fp);

		}

	printf("end\n");

}// the end of the program





RETURN