// Calculation Program for displaying a single egg curve and the accompanied curve (Suugaku-Dokusyuu-Juku's), May 27, '11 (Fri.)



// ( a x2 + b x + c )2 / ( d x + e )2 + ( y / f )2 = 1

// Recommended equation: ( ( x - h ) ( g x + 1 ) )2 / ( h ( p x + 1) )2 + ( y / f )2 = 1, Egg curve exists in x ≧0 and the accompanied curve reviels in x < 0.

// Recommended values of parameters: f=1.1, g=1.0, h=1.35 and p=1.3



// file name: egg_curve_by_Asai_III.c



#include< stdio.h>

#include< math.h>



void main(void)

{

	double x,y;

	double f,g,h,p;

	int i,imax,j,m;

	double x1,x2,x3,x4,dx;

	double xx[10001],yy[10001];



	FILE *fp;



// Setting of the constants

	f=1.1; //  Recommended value: f=1.1

        g=1.0; //  Recommended value: g=1.0

	h=1.35; // Recommended value: h=1.35

	p=1.3; //  Recommended value: p=1.3



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



// Setting of the other parameters

	x1=-(1.0+h*p-g*h)/g;

	x2=(h*(g+p)-1.0-sqrt((1.0-h*(g+p))*(1.0-h*(g+p))+8.0*g*h))/(2*g);

	x3=0;

	x4=(h*(g+p)-1.0+sqrt((1.0-h*(g+p))*(1.0-h*(g+p))+8.0*g*h))/(2*g);



	dx=(x2-x1)/4000;// plotting interval of x



// Execution of calculation to obtain coordinates data of egg curve

	i=0;

	for(x=x1+dx;x<=x2;x=x+dx)

		{

		i++;



		xx[i]=x;



		y=f*sqrt(1.0-((x-h)*(g*x+1.0)/(h*(p*x+1.0)))*((x-h)*(g*x+1.0)/(h*(p*x+1.0))));

		yy[i]=y;

		printf("i=%d,x=%f,y=%f\n",i,x,y);

		}

	imax=i;

	j=0;



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

		{

		j++;

		m=imax-j;



		xx[i]=xx[m];

		yy[i]=-yy[m];

		}



// Writing the calculated coordinates data of the egg curve into a textfile "egg-shaped curve.txt"

	fp=fopen("egg-shaped curve.txt","w");

	if(fp==NULL)

		{

		printf("FILE OPEN ERROR\n");

		}

	else

		{

		for(i=1;i<=2*imax-1;i++)

			{

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

			}

		fflush(fp);

		fclose(fp);

		}

	printf("end\n");



// Another setting of the plotting interval of x

	dx=(x4-x3)/4000;



// Execution of calculation to obtain coordinates data of the accompanied curve

	i=0;

	for(x=x3+dx;x<=x4;x=x+dx)

		{

		i++;



		xx[i]=x;



		y=f*sqrt(1.0-((x-h)*(g*x+1.0)/(h*(p*x+1.0)))*((x-h)*(g*x+1.0)/(h*(p*x+1.0))));

		yy[i]=y;

		printf("i=%d,x=%f,y=%f\n",i,x,y);

		}

	imax=i;

	j=0;



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

		{

		j++;

		m=imax-j;



		xx[i]=xx[m];

		yy[i]=-yy[m];

		}



// Writing the calculated coordinates data of the accompanied curve into a textfile "egg-shaped curve_2.txt"

	fp=fopen("egg-shaped curve_2.txt","w");

	if(fp==NULL)

		{

		printf("FILE OPEN ERROR\n");

		}

	else

		{

		for(i=1;i<=2*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