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



// file name: flower_d.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;



	int n;

	double t;

	double b,c;

	double d;

	double e;

	int i,imax,j;

	double xx[20001],yy[20001];

	double t1, t2;



	FILE *fp;



// setting of the constants

	pi=3.14159265;

	a=1;



	b=2.;

	c=0.1;

	d=1;

	e=0.;



	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;



	i=0;



// execution of 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);



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



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

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



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

		}

	}

	

	imax=i;



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

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