// Calculation Program for calculating shape of a flower with the use of a Cardioid (1), July 15, 2012



// file name: flower_g.c



#include< stdio.h>

#include< math.h>



void main(void)

{

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

	double t,dt;// the phase angle and its increment of the Cardioid before the conversion respectively

	double r,z;// the moving radius and the phase angle of the Cardioid after some conversion respectively

	double f;// the phase angle of the Cardioid after the final conversion into a horned one (using as a petal of a flower)

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

	double alpha;// the angle for the horned Cardioid (used as that alpha=2*pi/n (using as "alpha=pi/n" in this C program)

	double tmin,tmax;// the minimum and maximum values of the phase angle "t" respectively

	int i,imax,j;

	double x,y;// the orthogonal coordinates of a petal before contraction or expansion

	float b;// the ratio of contraction or expansion of a petal in the length direction

	float c;// the radius of a center circle

	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 

	double rr;// the moving radius of a petal after contraction or expansion

	double ff;// the phase angle of a petal after contraction or expansion

	double p;// phase angle of center circle

	double dp;// increment of "p"

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

	int m;// count numbers of the increment of "beta"

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



	FILE *fp;



// setting of the constants

	pi=3.14159265;

	a=1.;



// setting of the other parameters

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

	scanf("%d",&n);

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

	printf("\n");



	alpha=2*pi/n;// the angle for the horned Cardioid as a petal

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



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

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



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

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



	printf("Input the ratio of contraction or expansion of a petal in the length direction. \n b= ? ");

	scanf("%f",&b);

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

	printf("\n");



	printf("Input the radius of the center circle. \n c= ? ");

	scanf("%f",&c);

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

	printf("\n");



	tmin=-pi/2;

	tmax=3*pi/2;



	nz=200;



	dt=(tmax-tmin)/nz;// // plotting interval of "t"

	dbeta=beta/nz;// 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(t=tmin;t<=tmax+dt;t=t+dt) // sweep of phase angle "t" of the each petal

		{

			i++;

			m++;



			if(t>(3*pi/2-dt) && t<(3*pi/2+dt))

			{

				r=0.;

				z=-pi/2;

			}

			else

			{

				if(t>(-pi/2-dt) && t<(-pi/2+dt))

				{

					r=0.;

					z=pi/2;

				}

				else

				{

					r=a*sqrt((5-3*sin(t))*(1+sin(t)));

					z=asin(a*(1-sin(t))*cos(t)/r);

				}

			}

			

			f=-alpha*z/pi+pi/2;



			x=r*cos(f);

			y=b*r*sin(f);



			rr=sqrt(x*x+y*y);



			if(fpi||ff<0)

					{

						ff=atan(b*tan(f));

					}

*/

				}

			}



			ff=ff+2*pi*(j-1)/n;



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

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

		}



		if(l<=1) // Simply for drawing an additional circle of underlying petal.

		{

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

			{

				i++;



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

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



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

			}

		}

		else

		{

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

			{

				i++;



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

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



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

			}

		}

	}



	imax=i;



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

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