// Calculation Program for calculating shape of a flower with the use of a Cardioid_c1_overlap, Sep. 20, 2012



// file name: flower_c1_overlap.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 r,f;// the moving radius and the phase angle [radian] of a heart curve respectively

	double fmin,fmax,df;

	double x,y;

	double rr,ff;

	double ffmin,ffmax;

	int n;

	double t;

	double b,c;

	double d;

	double e;

	double k;

	double p;// the index of overlap of petals

	int i,imax,j;

	double xx[20001],yy[20001];

	double t1, t2,fff,fs,dfs,fff0,fffs;



	FILE *fp;



// setting of the constants [Caution: Some combination of conrtants b, c, d and e may yields a calculation error.  But, the cause of this error is unknown yet.]

	pi=3.14159265;

	a=1;



	b=2;

	c=0.5;

	d=1;

	e=0.5;

	k=1;

	p=2;// 0< p< 1 in the case of no overlap of petals, and 1< p in the case of overlap of petals



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

	scanf("%d",&n);

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

	printf("\n");



// setting of the other parameters

	fmin=-pi/2;

	fmax=3*pi/2;



	df=(fmax-fmin)/400;



	fmin=fmin+df;

	fmax=fmax-df;



	ffmin=pi/2;

	ffmax=pi/2;



// pre-calculation (obtaining the maximum and the minimum angles of the shape of heart curve)



	for(f=fmin;f<=fmax;f=f+df)

	{

		if(f<=-pi/2)

		{

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



		r=a*(1-sin(t));



		x=r*(1+c*sin(f))*cos(f);

		y=d*r*(1+c*sin(f))*sin(f)+2*a*d*(1-c);



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



		if(x==0)

		{

			ff=pi/2;

		}

		else

		{

			if(x>0)

			{

				ff=asin(y/rr);

			}

			else

			{

				ff=pi-asin(y/rr);

			}

		}

				

		if(ff>ffmax){ffmax=ff;}

		if(ff< ffmin){ffmin=ff;}

	}



	ffmax=k*(ffmax-pi/2)+pi/2;

	ffmin=k*(ffmin-pi/2)+pi/2;



	i=0;



// main calculation

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

	{

		for(f=fmin;f<=fmax;f=f+df)

		{

			i++;



			if(f<=-pi/2)

			{

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

			r=a*(1-sin(t));



			x=r*(1+c*sin(f))*cos(f);

			y=d*r*(1+c*sin(f))*sin(f)+2*a*d*(1-c);



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



			if(x==0)

			{

				ff=pi/2;

			}

			else

			{

				if(x>0)

				{

					ff=asin(y/rr);

				}

				else

				{

					ff=pi-asin(y/rr);

				}

			}





			fff=(p*2*pi/(n*(ffmax-ffmin)))*ff+pi/2+2*(j-1)*pi/n-p*pi*pi/(n*(ffmax-ffmin));



			if(i==1&&j==1) // connecting by an arc curve to fill a gap between adjacent petals (start)

			{

				fff0=fff;

			}



			if(fabs(fff-fffs)> 0.00001)

			{

				if(f==fmin&&j>1&&fffs>fff)

				{

					dfs=(fffs-fff)/20;

					for(fs=fffs;fs>=fff;fs=fs-dfs)

					{

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

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

						i++;

					}

				}

				if(f==fmin&&j>1&&fffs< fff)

				{

					dfs=(fff-fffs)/20;

					for(fs=fffs;fs<=fff;fs=fs+dfs)

					{

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

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

						i++;

					}

				}

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



			fffs=fff;



			xx[i]=(rr+e)*cos(fff);

			yy[i]=(rr+e)*sin(fff);



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

		}

	}



	if(fffs>2*pi) // connecting by an arc curve to fill a gap between adjacent petals (start)

	{

		fffs=fffs-2*pi;

	}



	if(fabs(fff-fffs)> 0.00001)

	{

		if(fffs>fff0)

		{

			dfs=(fffs-fff0)/20;

			for(fs=fffs;fs>=fff0;fs=fs-dfs)

			{

				i++;

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

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

			}

		}

		else

		{

			dfs=(fff0-fffs)/20;

			for(fs=fffs;fs<=fff0;fs=fs+dfs)

			{

				i++;

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

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

			}

		}

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



	i++;

	xx[i]=xx[1];

	yy[i]=yy[1];



	imax=i;



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

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