// Calculation Program for calculating shape of a flower with the use of Cardioid (d_independent_angle), Sep. 28, 2023



// file name: flower_d_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 t;// the phase angle [radian] of Cardioid before the conversion

	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 t1, t2;// intermediate variables



	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=2.;

	c=0.5;

	d=1;

	e=0.4;



	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)/nf;// 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++;



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

			{

				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);// conversion of the phase angle from Cardioid to a heart curve (the end)



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



			x=r*(1+c*sin(f))*cos(f); // deformation of heart curve and expression into the orthogonal coordinates

			y=d*r*(1+c*sin(f))*sin(f)+2*a*d*(1-c); // 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_d_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