// Calculation Program for calculating shape of a flower with the use of a circle (flower_fzz5_independent_angle), Oct. 15, 2012



// file name: flower_fzz5_independent_angle.c



#include< stdio.h>

#include< math.h>



void main(void)

{

	double a,pi;// "a" is the constant of the original Cardioid, and "pi" is the pi

	double r,f;// the moving radius and the phase angle [radian] of a heart curve respectively

	double fmin,fmax,df;// the minimum, maximum values and increment of the phase angle "f" respectively

	double x,y;// the orthogonal coordinates of a heart curve (used as a petal)

	double rr,ff;// the moving radius and the phase angle of a petal after contraction or expansion respectively



	int n;// a desired numbers of petals of a flower

	double b,c;// conversion coefficients from Cardioid to a heart curve

	double d;// expansion coefficient of Cardioid  in the length direction

	double e;// the radius of a center circle

	int i,imax,j;

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



	double p;// phase angle of the center circle

	double dp;// increment of "p"

	double beta;// an arbitral width of phase angle with which each petal contacts to the center circle

	double dbeta;// increment of "beta"

	float l;// the correction factor for "beta" ( 0 < l )

	int m;// counting number of "dbeta"

	int nf;// devision number of "f" and "beta"



	FILE *fp;



// setting of the constants

	pi=3.14159265;

	a=1;



	b=.5;// 0.5

	c=4;// 3

	d=3;// 0.5 to 1.5

	e=2;// 0.3 to 2



	printf("Input a desired numbers of petals of a flower in natural number. \n n=? ");

	scanf("%d",&n);

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

	printf("\n");



	printf("Input the correction factor for 'beta'. \n l= ? ( 0 < l )");

	scanf("%f",&l);

	printf("l=%f\n",l);



	beta=2*pi/n;// fundamental equivalence

	beta=l*beta;// corrected width of phase angle with which each petal contacts to the center circle



// setting of the other parameters

	fmin=-pi/2;

	fmax=3*pi/2;



	nf=400;



	df=(fmax-fmin)/400;// plotting interval of phase angle "f" before conversion



	dbeta=beta/nf;// increment of beta



	dp=2*pi/n/20;// increment of p



// execution of calculation



	i=0;



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

	{

		m=0;



		for(f=fmin;f<=fmax;f=f+df) // sweep of phase angle "f" of the each petal

		{

			i++;

			m++;



			r=a;



			x=c*r*cos(f); // deformation of the cicle and expression into the orthogonal coordinates

			y=d*r*sqrt(1+b*(1+sin(f)))*sin(f)+d*a; // deformation of heart curve, displacement of the origin point in the coordinates and expression into the orthogonal coordinates



			rr=sqrt(x*x+y*y);// calcuration of the moving radius after conversion



			ff=(f+(2*pi*j+(n-5)*pi/2))/n; // conversion of the phase angle from a single petal ("f") into the whole petals of a flower ("ff")

			

			xx[i]=rr*cos(ff)+e*cos(2*pi*(j-1)/n+pi/2-beta/2+m*dbeta);

			yy[i]=rr*sin(ff)+e*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]);

		}



		if(l<1) // connecting by an arc curve to fill a gap between adjacent petals (start)

		{

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

			{

				i++;



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

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



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

			}

		}

		else

		{

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

			{

				i++;



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

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



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

			}

		} // connecting by an arc curve to fill a gap between adjacent petals (the end)

	}

	

	imax=i;



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

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



// writing the calculated coordinates data of the curve into a textfile

	fp=fopen("flower_fzz5_independent_angle.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