// Calculation Program for calculating shape of a flower
// with the use of a circle (part C), July 4th, 2012



// file name: flower_curve_C.c



#include< stdio.h>

#include< math.h>



void main(void)

{

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

	double r,z;
// "r" and "z"(-π/2 =< z =< π/2)are the moving radius
// and the phase angle of petal respectively.

	double dz;// increment of "z"

	double f;
// the phase angle of petal made of
// a sharpened original circle

	double p;// phase angle of center circle

	double dp;// increment of "p"

	int n;// numbers of petals

	double alpha;
// the sharp angle of the bottom of the petal
// (The criterion value is "alpha=2*pi/n".)

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

	double dbeta;// beta の変分

	float k;
// magnification factor k ( >0 )
// of the criterion angle "alpha=2*pi/n".

	float l;
// magnification factor l ( >0 )
// of the angular range "beta".

	int m;

	int nz;
// division numbers of both phases "z" and "beta"

	double zmin,zmax;
// the minimum and the maximum values
// of phase angle "z" respectively

	int i,imax,j;

	float c;// the radius of the center circle

	double xx[30001],yy[30001];
// Take care of the upper limit of
// storage memory capacitance.



	FILE *fp;



// setting of the constants

	pi=3.141592;

	a=1.;



// setting of the other parameters

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

	scanf("%d",&n);

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



	alpha=2*pi/n;
// the shsarpened angle of the botom of a petal

	beta=alpha;
// fundamentally criteria value of "beta" is supposed
// to be equal to "alpha".



printf("Input magnification factor k ( > 0 ), \n k= ? ");
printf("where k is multiplied by");
printf("the criterion angle 'alpha=2*pi/n'.");


	scanf("%f",&k);

	printf("h=%f\n",k);



	alpha=k*alpha;
// the magnificated angle of the botom of the petal



printf("Input magnification factor l ( >0 ) \n l= ? ");
printf("where l is multiplied by");
printf("the criterion angular range 'beta'.");

	scanf("%f",&l);

	printf("l=%f\n",l);



	beta=l*beta;
// the magnificated angular range to be occupied
// by a petal around the center circle



printf("Input the radius of the center circle. \n c= ? ");

	scanf("%f",&c);

	printf("c=%f\n",c);



	zmin=-pi/2;

	zmax=pi/2;



	nz=200;



	dz=(zmax-zmin)/nz;// plotting interval of "z"



	dbeta=beta/nz;// plotting interval of "beta"



	dp=2*pi/n/20;// plotting interval of "p"



// execution of calculation

	i=0;



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

	{

		m=0;



	for(z=zmin;z<=zmax+dz;z=z+dz) 
        // sweep of phase angle "z" of the each petal

		{

			i++;

			m++;



			r=2*a*cos(z);

			

			f=-alpha*z/pi+pi/2-2*pi*(j-1)/n;



xx[i]=r*cos(f)+c*cos(-2*pi*(j-1)/n+pi/2+beta/2-m*dbeta);

yy[i]=r*sin(f)+c*sin(-2*pi*(j-1)/n+pi/2+beta/2-m*dbeta);



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

	}



		if(l<1) 
// connecing by an arc curve to fill each gap
// between adjacent petals (start)

	{

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

	{

			i++;



			xx[i]=c*cos(-p+pi/2);

			yy[i]=c*sin(-p+pi/2);



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

		}

		}

		else

		{

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

	{

			i++;



			xx[i]=c*cos(-p+pi/2);

			yy[i]=c*sin(-p+pi/2);



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

	}

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

	}

	

	imax=i;



	xx[imax+1]=xx[1];

	yy[imax+1]=yy[1];





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

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

	if(fp==NULL)

		{

		printf("FILE OPEN ERROR\n");

		}

	else

		{

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

			{

		fprintf(fp,"%f,%f\n",xx[i],yy[i]);

			}

		fflush(fp);

		fclose(fp);

		}

	printf("end\n");

}// the end of the program





RETURN