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



// file name: flower_x3.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 r,t;// the moving radius and the phase angle of the original Cardioid respectively

	double f,df;// the phase angle and its increment of heart curve respectively

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

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

	double x,y;// the orthogonal coordinates of a petal (which is a heart curve itself) before contraction or expansion

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

	double b,c;// reformation coefficients of a petal

	double d;// compression coefficient of a petal in the length direction 

	double e,p;// the radius  and the phase angle of a center circle respectively

	double dp;// the increment of "p"

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

	double dbeta;// the increment of "beta"

	double k;// coefficient for to vary the bottom angle of the heart curve

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

	int nf;// division numbers of both phase angles "t" and "beta"

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

	int i,imax,j;

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

	double t1, t2;// auxiliary variables to mediate "t"





	FILE *fp;



// setting of the constants

	pi=3.14159265;

	a=1;



	b=1;// b=1

	c=0.2;// c=-0.2.   [Caution] It is required that -1 < c < 1.

	d=1;// d=1

	e=0.3;// e=0.3

	k=0.7;// k=0.7

	l=1;// l=1



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

	scanf("%d",&n);

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

	printf("\n");



	beta=2*pi/n;// fundamentally criteria value of angular range to be occupied by a petal around the center circle

	beta=l*beta;// corrected value of "beta" with "l"



// setting of the other parameters

	fmin=-pi/2;

	fmax=3*pi/2;



	nf=200;



	df=(fmax-fmin)/nf;// plotting interval of "f"

	dbeta=beta/nf;// increment of "beta"



	dp=2*pi/n/100;// increment of "p"



	i=0;



// execution of calculation

	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++;



			if(f<=-pi/2)// start of conversion of the phase from Cardioid to heart curve

			{

				t1=0;

			}

			else

			{

				t1=b*sqrt(f+pi/2);

			}

			if(f>=3*pi/2)

			{

				t2=0;

			}

			else

			{

				t2=b*sqrt(3*pi/2-f);

			}

			

			t=t1-t2+(1-b*sqrt(2/pi))*f+b*sqrt(pi/2);// end of conversion of the phase from Cardioid to heart curve



			r=a*(1-sin(t));// relationship between the moving radius of the heart curve and the phase angle of Cardioid



			x=r*(1+c*sin(f)*cos(f)*cos(f))*cos(f);// x coordinates of the fundamental (1st) petal (reformed heart curve)

			y=d*r*(1+c*sin(f)*cos(f)*cos(f))*sin(f)+2*a*d;// y coordinates of the fundamental (1st) petal (reformed heart curve)



			rr=sqrt(x*x+y*y);// calculation of the moving radius of the above petal (reformed heart curve)



			if(x==0)// calculation of the phase angle of the above petal (reformed heart curve)

			{

				ff=pi/2;

			}

			else

			{

				if(x>0)

				{

					ff=asin(y/rr);

				}

				else

				{

					ff=pi-asin(y/rr);

				}

			}

			

			ff=k*(ff-pi/2)+pi/2;// to vary the bottom angle of petal (the reformed heart curve)



			ff=ff+2*pi*(j-1)/n;// the phase angle of the j-th petal



			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) // 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+0;p=p+dp)

			{

				i++;



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

				yy[i]=e*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-0;p=p-dp)

			{

				i++;



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

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



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

			}

		}



	}



	i++;

	xx[i]=xx[1];// for connecting the start and the end points

	yy[i]=yy[1];



	for(p=pi/2-beta/2;p<2*pi*(n-1)/n+pi/2+beta/2-0;p=p+dp)// drawing a center circle

	{

		i++;

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

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

	}



	imax=i;



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

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