// Calculation Program for calculating shape of a flower with the use of a circle (part D2), Sep. 9th, 2012



// file name: circle_flower_D2.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;// increment of "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;// the count number of "dbeta"

	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=0.7;//b=0.7



// 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=1.1;// k=1.1 //magnification factor of the criterion angle 'alpha=2*pi/n' where 0 < k



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



	l=0;// l=0 // 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=0; // c=0 // the radius of the center circle





	zmin=-pi/2;

	zmax=pi/2;



	nz=500;



	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)*cos(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_D2.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