// Calculation Program for simulation in the case that the tip of the petals are pointed and also the base of the petals become thin,  flower_y1, June 17, 2013



// file name: flower_y1.c



#include< stdio.h>

#include< math.h>



void main(void)

{

	double a,pi;// "a" is the constant of the original curve of petal and pi=3.1415...

	double r;// the moving radius of the original curve of petal

	double q1,q2,b;// constants giving degrees of deformation to the original curve of petal

	double f,df;// the intermediate variable and its increment of the original curve of petal respectively

	double fmin,fmax;// the minimum and maximum values of "f" respectively

	int nf;// division numbers of interval between "fmin" anf "fmax"

	double x,y;// the orthogonal coordinates of the original curve of petal



	double rr,ff;// the moving radius and the phase angle of petal of provisional respectively



	int n;// desired number of petals of a flower

	double d;// compression coefficient of a petal in the length 

	double k;// coefficient for to vary the base angle of petal



	double e,p;// the radius and the phase angle of a center circle respectively

	double dp;// the increment of "p"

	double beta;// angular range to be occupied by a petal around the center circle

	double l;// magnification factor of the angular range "beta" where 0 < l 

	double dbeta;// the increment of "beta"



	double xx,yy;// intermediate variables

	double rrr,fff;// the moving radius and the phase angle of petal



	double xxx[20001],yyy[20001];// the orthogonal variables of petal, Take care of the upper limit of storage memory capacitance.



	int i,imax,j;

	int m;// count number of the increment of "beta"



	FILE *fp;



// setting of the constants

	pi=3.14159265;



	a=1;//    a=1

	q1=1;//   q1=1

	q2=2;//   q2=2

	b=0.1;//  b=0.1

	d=8;//    d=8

	e=0.0;//  e=0

	k=1;//    k=1

	l=0.2;//  l=0.2



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

	scanf("%d",&n);

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

	printf("\n");



	beta=2*pi/n;// fundamental criteria value of angular range to be occupied by a petal around the center circle

	beta=l*beta;// corrected value of "beta" with "l"



// setting of the other parameters

	fmin=-pi/2;

	fmax=3*pi/2;



	nf=100;



	df=(fmax-fmin)/nf;// plotting interval of "f"

	dbeta=beta/nf;// increment of "beta"



	dp=2*pi/n/100;// increment of "p"



	i=0;



// execution of calculation

	for(j=1;j<=n;j++) // sweep of n number of petals

	{

		m=0;



		for(f=fmin;f< fmax;f=f+df) // sweep of "f"

		{

			i++;

			m++;



			r=(a/(1+(1/q1)*(sqrt(fabs(sin(f/2-pi/4))))))/(1+(1/q2)*sqrt(fabs(sin(f/2+pi/4))));// the moving radius of the original curve of petal



			x=r*cos(f);// x coordinates of the original curve of petal

			y=b*r*sin(f)+b*(a/(1+(1/q1)));// y coordinates of the original curve of petal

			rr=sqrt(x*x+y*y);// the moving radius of petal of provisional



			if(x==0)// the phase angle of petal of provisional

			{

				ff=pi/2;

			}

			else

			{

				if(x>0)

				{

					ff=asin(y/rr);

				}

				else

				{

					ff=pi-asin(y/rr);

				}

			}



			ff=k*(ff-pi/2)+pi/2;// to vary the base angle of petal



			xx=rr*cos(ff);

			yy=d*rr*sin(ff);



			rrr=sqrt(xx*xx+yy*yy);// the moving radius of petal





			if(xx==0)// the phase angle of petal

			{

				fff=pi/2;

			}

			else

			{

				if(xx>0)

				{

					fff=asin(yy/rrr);

				}

				else

				{

					fff=pi-asin(yy/rrr);

				}

			}





			fff=fff+2*pi*(j-1)/n;// the phase angle of the j-th petal



			xxx[i]=rrr*cos(fff)+e*cos(2*pi*(j-1)/n+pi/2-beta/2+m*dbeta);

			yyy[i]=rrr*sin(fff)+e*sin(2*pi*(j-1)/n+pi/2-beta/2+m*dbeta);



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

		}



		if(l<=1)// drawing a center circle

		{

			for(p=2*pi*(j-1)/n+pi/2+beta/2;p< 2*pi*j/n+pi/2-beta/2+0;p=p+dp)

			{

				i++;



				xxx[i]=e*cos(p);

				yyy[i]=e*sin(p);



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

			}

		}

		else

		{

			for(p=2*pi*(j-1)/n+pi/2+beta/2;p>2*pi*j/n+pi/2-beta/2-0;p=p-dp)

			{

				i++;



				xxx[i]=e*cos(p);

				yyy[i]=e*sin(p);



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

			}

		}

	}



	i++;

	xxx[i]=xxx[1];// for connecting the start and the end points

	yyy[i]=yyy[1];



	imax=i;



// writing the calculated coordinates data of the curve into a textfile named "flower_y1.txt"

	fp=fopen("flower_y1.txt","w");

	if(fp==NULL)

	{

		printf("FILE OPEN ERROR\n");

	}

	else

	{

		for(i=1;i<=imax;i++)

		{

			fprintf(fp,"%f,%f\n",xxx[i],yyy[i]);

		}

		fflush(fp);

		fclose(fp);

	}

	printf("end\n");

}// the end of the program





RETURN