// Calculation Program b for calculating shape of a flower with the use of Cardioid (b_independent_angle). Sep. 26, 2012



// file name: flower_b_independent_angle.c



#include< stdio.h>

#include< math.h>



void main(void)

{

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

	double t,dt;// the phase angle [radian] before the conversion and its increment [radian] of a Cardioid

	double r,z;// the moving radius anf the phase angle of Cardioid respectively after removement of its origin point

	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 )

	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 rr;// the moving radius of a petal after contraction or expansion

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

	double xx[30001],yy[30001];// 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.;



// setting of the other parameters

	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");



	alpha=2*pi/n;



	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 a center circle. \n c= ? ");

	scanf("%f",&c);

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

	printf("\n");



	printf(" 0< p< 1 in the case of no overlap of petals, and 1< p in the case of overlap of petals. \n l= ? ");

	scanf("%f",&l);

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



	beta=alpha;// fundamental equivalence

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



	tmin=-pi/2;

	tmax=3*pi/2;



	nf=200;



	dt=(tmax-tmin)/nf;// plotting interval of phase angle "t" 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++) // the sweeping of n numbers of petals

	{



		m=0;



		for(t=tmin;t<=tmax+dt;t=t+dt) // the sweeping of the phase angle t of a single petal

		{

			i++;

			m++;



			if(t>(3*pi/2-dt) && t<(3*pi/2+dt)) // conversion of moving radius and phase angle (start)

			{

				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(f< pi/2)

			{

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

			}

			else

			{

				if(f==pi/2)

				{

					ff=pi/2;

				}

				else

				{

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

				}

			} // conversion of moving radius and phase angle (the end)



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



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

			yy[i]=rr*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) // 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]=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]);

			}

		}

		else

		{

			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]);

			}

		} // 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 into a textfile

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