// Calculation Program for simulation of a petal with pointed tip and also with thin base,  flower_y1pre, June 17, 2013



// file name: flower_y1pre.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 d;// compression coefficient of a petal in the length 

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

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

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

	int i,imax;



	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=1

	k=1;//    k=1



// setting of the other parameters

	fmin=-pi/2;

	fmax=3*pi/2;



	nf=100;



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



	i=0;



// execution of calculation

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

	{

		i++;



		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



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

		{

			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[i]=rr*cos(ff);

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



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

	}

	

	i++;

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

	yy[i]=yy[1];



	imax=i;



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

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