// Calculation Program for displaying a single heart curve VI coversed from a Cardioid, 14 Apr., 2010

// file name: heart_curve_f.c

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

void main(void)
{
	double a,pi;// a is the constant of the original Cardioid, pi is the pi
	double t,dt;// the phase angle [radian] and its increment [radian] of a Cardioid before the conversion respectively
	double r,z;// the moving radius and the phase angle [radian] of a Cardioid after some conversion respectively
	double f;// the phase angle [radian] of a Cardioid after the final conversion into a horned one
	float angle;// the desired angle [degree] for the horned Cardioid
	float nb;// compression rate [percent] in the vertical direction
	double alpha;// the unit conversion of the variable "angle" from "degree" to "radian"
	double b;// the compression coefficient in the vertical direction
	float p;// newly introduced constant in addition to the previous method I
	double tmin,tmax;// the minimum and maximum values of the phase angle "t" respectively
	int i,imax;
	double x,y;// coordinates variables obtained likely as in the previous method I
	double xx[10001],yy[10001];// Take care of the upper limit of storage memory capacitance.

	FILE *fp;

// setting of the constants
	pi=3.14159265;
	a=1;

// setting of the other parameters
	printf("Input of the desired angle [degree] for the horned Cardioid (positive integer less than 180 degree); alpha [degree] = ? ");
	scanf("%f",&angle);
	printf("\n");
	alpha=pi*angle/180;

	printf("Input of the compression rate [percent] in the vertical direction (suitable at between 30 and 100); b [percent] = ? ");
	scanf("%f",&nb);
	printf("\n");
	b=1.0*nb/100;

	printf("newly introduced constant (suitable at between 0 and 0.5); p = ? ");
	scanf("%f",&p);

	tmin=-pi/2;
	tmax=3*pi/2;
	dt=(tmax-tmin)/4000;// plotting interval of the phase angle "t" before conversion

// execution of calculation
	i=0;

	for(t=tmin;t<=tmax+dt;t=t+dt)
	{
		i++;
		if(t>(3*pi/2-dt) && t<(3*pi/2+dt))
		{
			r=0.;
			z=-pi/2;
		}
		else
		{
			if(t>(-pi/2-dt) && t<(-pi/2+dt))
			{
				r=0.;
				z=pi/2;
			}
			else
			{
				r=a*sqrt((5-3*sin(t))*(1+sin(t)));
				z=asin(a*(1-sin(t))*cos(t)/r);
			}
		}

		f=-alpha*z/pi+pi/2;

		x=r*cos(f);
		xx[i]=x;
		y=b*r*sin(f);

		if(x>=0)
		{
			yy[i]=y+p*sqrt(x);
		}
		else
		{
			yy[i]=y+p*sqrt(-x);
		}

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

	imax=i;

// writing the calculated coordinates data of the curve into a textfile
	fp=fopen("heart_curve_f.txt","w");
	if(fp==NULL)
		{
		printf("FILE OPEN ERROR\n");
		}
	else
		{
		for(i=1;i<=imax;i++)
			{
			fprintf(fp,"%f,%f\n",xx[i],yy[i]);
			}
		fflush(fp);
		fclose(fp);
		}
	printf("end\n");
}// the end of the program



RETURN