// Calculation Program for displaying five species of egg-shaped-curves simultaneousely, 9(Mon.) Jan., 2004
// Equation of the curve: (x*x+y*y)**2=a*(x**3)+(a-b)*x*(y**2), In general,  b<=a is necessary.  When b=0, the curve is a circle.
// file name: egg_shaped_curve_2E.c

#include< stdio.h>
#include< math.h>

void main(void)
{
	double x,y,a,b,db,bmax;
	int i,imax,n,nmax,j,m;
	double xmax,dx;
	double xx[2005],yy[10][1005];// Take care of the upper limit of storage memory capacitance.

	FILE *fp;

// setting of the constant
        a=4.0;

// setting of the other parameters
	xmax=a;// the maximum value of x
	dx=xmax/400;// plotting interval of x
	bmax=a;
	db=bmax/5;

	i=0;

// execution of calculation
	for(x=0;x<=xmax;x=x+dx)
	{
		i++;
		n=0;

		xx[i]=x;

		for(b=0;b<=bmax;b=b+db)
		{
			n++;

			y=sqrt(((a-b-2*x)+sqrt(4*b*x+(a-b)*(a-b)))*x/2);// It becomes the equation of a circle: y=sqrt(a*a/2/2-(x-a/2)*(x-a/2)) when b=0.
			yy[n][i]=y;

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

	imax=i;
	nmax=n;

	j=0;

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

		xx[i]=xx[m];

		for(n=1;n<=nmax;++n)
		{
			yy[n][i]=-yy[n][m];
		}
	}

// writing the calculated coordinates data of the curves into a textfile
	fp=fopen("egg_shaped_curve.txt","w");

	if(fp==NULL)
	{
		printf("FILE OPEN ERROR\n");
	}
	else
	{
		for(i=1;i<=2*imax;i++)
		{
			fprintf(fp,"%f,%f,%f,%f,%f,%f,%f\n",xx[i],yy[1][i],yy[2][i],yy[3][i],yy[4][i],yy[5][i],yy[6][i]);
		}

		fflush(fp);
		fclose(fp);
	}

	printf("end\n");
}// the end of the program



RETURN