// C program for calculating shape of a flower (a_overlap). Sep. 19, 2012



// file name: flower_curve_a_overlap.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 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 )

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

	int i,imax,j;

	float c;// the radius of a center circle

	float p;// the index of overlap of petals

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

	double ff,fs,dfs,ff0,ffs;// intermediates variables



	FILE *fp;



// setting of the constants

	pi=3.141592;

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



	alpha=2*pi/n;



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

	scanf("%f",&c);

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



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

	scanf("%f",&p);

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



	tmin=-pi/2;

	tmax=3*pi/2;

	dt=(tmax-tmin)/500;// plotting interval of the phase angle "t" before conversion



// execution of calculation

	i=0;



	for(j=1;j<=n;j++)

	{

		for(t=tmin;t<=tmax+dt;t=t+dt)

		{

			i++;



			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=-p*(alpha*z/pi)+pi/2+2*pi*(j-1)/n;



			ff=f;



			if(fabs(p-1.)>0.00001) // connecting by an arc curve to fill a gap between adjacent petals (start)

			{



				if(i==1&&j==1)

				{

					ff0=ff;

				}



				if(t==tmin&&j>1)

				{

					if(ffs>ff)

					{

						dfs=(ffs-ff)/20;

						for(fs=ffs;fs>=ff;fs=fs-dfs)

						{

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

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

							i++;

						}

					}

					else

					{

						dfs=(ff-ffs)/20;

						for(fs=ffs;fs<=ff;fs=fs+dfs)

						{

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

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

							i++;

						}

					}

				}				

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



			ffs=ff;



			xx[i]=(r+c)*cos(f);

			yy[i]=(r+c)*sin(f);



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

		}

	}



	if(fabs(p-1.)>0.00001) // connecting by an arc curve to fill a gap between adjacent petals (start)

	{

		if(ffs>2*pi)

		{

			ffs=ffs-2*pi;

		}

		

		if(ffs>ff0)

		{

			dfs=(ffs-ff0)/20;

			for(fs=ffs;fs>=ff0;fs=fs-dfs)

			{

				i++;

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

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

			}

		}

		else

		{

			dfs=(ff0-ffs)/20;

			for(fs=ffs;fs<=ff0;fs=fs+dfs)

			{

				i++;

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

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

			}

		}

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

	

	imax=i;



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

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