// Calculation Program for calculating shape of a flower with the use of Cardioid (c2_independent_angle), Sep. 27, 2012
// file name: flower_c2_independent_angle.c
#include< stdio.h>
#include< math.h>
void main(void)
{
double a,pi;// "a" is the constant of the original Cardioid, and "pi" is the pi
double r,f;// the moving radius and the phase angle [radian] of a heart curve respectively
double fmin,fmax,df;// the minimum, maximum values and increment of the phase angle "f" respectively
double x,y;// the orthogonal coordinates of a heart curve (used as a petal)
double rr,ff;// the moving radius and the phase angle of a petal after contraction or expansion respectively
double ffmin,ffmax;// the minimum and the maximum values of "ff" respectively
int n;// a desired numbers of petals of a flower
double t;// the phase angle [radian] of Cardioid before the conversion
double b,c;// conversion coefficients from Cardioid to a heart curve
double d;// expansion coefficient of Cardioid in the length direction
double e;// the radius of a center circle
double k;// deformation facter of width of a petal
int i,imax,j;
double xx[20001],yy[20001];// Take care of the upper limit of storage memory capacitance.
double t1, t2,fff;// intermediate variables
double p;// phase angle of the center circle
double dp;// increment of "p"
double beta;// an arbitral width of phase angle with which each petal contacts to the center circle
double dbeta;// increment of "beta"
float l;// the correction factor for "beta" ( 0 < l )
int m;// counting number of "dbeta"
int nf;// devision number of "f" and "beta"
FILE *fp;
// setting of the constants
pi=3.14159265;
a=1;
b=2.;
c=0.5;
d=1;
e=0.4;
k=1;
printf("Input a desired numbers of petals of a flower in natural number. \n n=? ");
scanf("%d",&n);
printf("n=%d\n",n);
printf("\n");
printf("Input the correction factor for 'beta'. \n l= ? ( 0 < l )");
scanf("%f",&l);
printf("l=%f\n",l);
beta=2*pi/n;// fundamental equivalence
beta=l*beta;// corrected width of phase angle with which each petal contacts to the center circle
// setting of the other parameters
fmin=-pi/2;
fmax=3*pi/2;
nf=400;
df=(fmax-fmin)/nf;// plotting interval of phase angle "f"
dbeta=beta/nf;// increment of beta
dp=(2*pi/n)/20;// increment of p
fmin=fmin+df;// modification of "fmin"
fmax=fmax-df;// modification of "fmax"
ffmin=pi/2;// unnecessary ?
ffmax=pi/2;// unnecessary ?
// pre-calculation (obtaining the maximum and the minimum angles of the shape of heart curve)
ffmin=atan(b*b*d/2);// obtaining by theoretical calcuration
ffmax=pi-ffmin;// obtaining by theoretical calcuration
ffmax=k*(ffmax-pi/2)+pi/2;
ffmin=k*(ffmin-pi/2)+pi/2;
// execution of main calculation
i=0;
for(j=1;j<=n;j++) // sweep of n numbers of petals
{
m=0;
for(f=fmin;f<=fmax;f=f+df) // sweep of phase angle "f" of the each petal
{
i++;
m++;
if(f<=-pi/2) // conversion of both of the moving radius and the phase angle from Cardioid to a heart curve (start)
{
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); // conversion of both of the moving radius and the phase angle from Cardioid to a heart curve (the end)
r=a*(1-sin(t)); // relationship between the moving radius and the phase angle of heart curve
x=r*(1+c*sin(f))*cos(f); // deformation of heart curve and expression into the orthogonal coordinates
y=d*r*(1+c*sin(f))*sin(f)+2*a*d*(1-c); // deformation of heart curve, displacement of the origin point in the coordinates and expression into the orthogonal coordinates
rr=sqrt(x*x+y*y);// calcuration of the moving radius after conversion
if(x==0)// calcuration of the phase angle after conversion
{
ff=pi/2;
}
else
{
if(x>0)
{
ff=asin(y/rr);
}
else
{
ff=pi-asin(y/rr);
}
} // conversion of both the moving radius and the phase angle from Cardioid to a heart curve (the end)
ff=k*(ff-pi/2)+pi/2;// expansion of the width of a single petal
fff=(2*pi/(n*(ffmax-ffmin)))*ff+pi/2+2*(j-1)*pi/n-pi*pi/(n*(ffmax-ffmin)); // conversion of the phase angle from a single petal ("ff") into the whole petals of a flower ("fff")
xx[i]=rr*cos(fff)+e*cos(2*pi*(j-1)/n+pi/2-beta/2+m*dbeta);
yy[i]=rr*sin(fff)+e*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) // connecting by an arc curve to fill a 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]=e*cos(p+pi/2);
yy[i]=e*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]=e*cos(p+pi/2);
yy[i]=e*sin(p+pi/2);
printf("i=%d,x=%f,y=%f\n",i,xx[i],yy[i]);
}
}// connecting 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 into a textfile
fp=fopen("flower_c2_independent_angle.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