// Calculation Program for calculating shape of a flower with the use of a Cardioid_c2,  23 Feb., 2009



// file name: flower_c2.c



#include< stdio.h>

#include< math.h>



void main(void)

{

	double a,pi;

	double r,f;

	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;

	int i,imax,j;

	double xx[20001],yy[20001];

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



	FILE *fp;



// setting of the constants

	pi=3.14159265;

	a=1;



	b=1.;

	c=0.5;

	d=1;

	e=0.5;

	k=1.;



	printf("Input of 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)

	ffmin=atan(b*b*d/2);

	ffmax=pi-ffmin;



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

				}

			}

			

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



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



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

			{

				fff0=fff;

			}



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

			{

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

					}

				}

				else

				{

					dfs=(fff-fffs)/20;

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

					{

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

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

						i++;

					}

				}				

			}



			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)

	{

		fffs=fffs-2*pi;

	}



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

		}

	}



	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_c.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