// Program for calculation of a spiral on an apple surface,Jan. 21, 2011.
// file name: apple_spiral.c
#include< stdio.h>
#include< math.h>
void main(void)
{
	double a,pi,c;// a is constant, pi is the Pi and c is adjustment coefficient for number of revolution of spiral.
	double b;// a compression constant towards y direction
	double p, q;// compression coefficients towards x and y directions respectively
	double t,dt;// an intermediate variable and its increment [radian] of Cardioid before the conversion
	double R;// radius of Cardioid before the conversion
	double tmin,tmax;// the minimum and maximum values of the intermediate variable t [radian]
	int i,imax;
	double xx[20001],yy[20001],zz[20001];// Take care of the upper limit of storage memory capacitance.
	FILE *fp;
// setting of the constant
	pi=3.14159265;
	a=1.0; // one of parameters in apple equation
	b=1.2; // one of parameters in apple equation
	p=0.15; // one of parameters in apple equation
	q=0.08; // one of parameters in apple equation
	c=50.0; // adjustment coefficient for number of revolution of spiral  
// setting of the other parameters
	tmin=-pi/2;
	tmax=pi/2;
	dt=(tmax-tmin)/400;// plotting interval of t
// execution of calculation
	i=0;
	for(t=tmin;t<=tmax+dt;t=t+dt)
    {
		i++;
		R=a*(1-sin(t));
		xx[i]=(R*cos(t)*exp(-p*(t-pi/2)*(t-pi/2)))*cos(c*t);
		yy[i]=(R*cos(t)*exp(-p*(t-pi/2)*(t-pi/2)))*sin(c*t);
		zz[i]=b*R*sin(t)*exp(-q*(t-pi/2)*(t-pi/2));
		printf("i=%d,x=%f,y=%f,z=%f\n",i,xx[i],yy[i],zz[i]);
	}
	imax=i;
// writing the calculated coordinates data of the apple spiral into a textfile
	fp=fopen("apple_spiral0.xls","w");
	if(fp==NULL)
	{
		printf("FILE OPEN ERROR\n");
	}
	else
	{
		for(i=1;i<=imax;i++)
		{
			fprintf(fp,"%f,%f,%f\n",xx[i],yy[i],zz[i]);
		}
		fflush(fp);
		fclose(fp);
	}
	printf("end\n");
}// the end of the program
RETURN