// Calculation Program for calculating shape of a flower with the use of a Cardioid_e2, edited in Mar. 05, 2009, & improved in Sep. 11, 2012.

// file name: flower_e2.c

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

void main(void)
{
	double a,pi;
	double r,f;
	double fmin,fmax,df;
	double x,y;
	double rr,ff;

	int n;
	double d;
	double e;
	double p;// the index of overlap of petals
	int i,imax,j;
	double xx[20001],yy[20001];
	double fs,dfs,ff0,ffs;

	FILE *fp;

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

	d=2;
	e=1.;
	p=1.5;// 0< p< 1 in the case of no overlap of petals, and 1< p in the case of overlap of petals

	printf("Input of numbers of petals. \n n=? ");
	scanf("%d",&n);
	printf("n=%d\n",n);
	printf("\n");

// setting of the other parameters
	fmin=-pi/2;
	fmax=3*pi/2;

	df=(fmax-fmin)/400;

	i=0;

// execution of calculation
	for(j=1;j<=n;j++)
	{
		for(f=fmin;f<=fmax;f=f+df)
		{
			i++;

			r=a*(1-sin(f));

			x=r*cos(f);
			y=d*r*sin(f)+2*a*d;

			rr=sqrt(x*x+y*y);

			ff=(f+(2*pi*j/p+((n/p)-5)*pi/2))/(n/p);
			
			if(fabs(p-1.)>0.00001) // connecting by an arc curve to fill a gap between adjacent petals (start)
			{

				if(i==1&&j==1)
				{
					ff0=ff;
				}

				if(f==fmin&&j>1)
				{
					if(ffs>ff)
					{
						dfs=(ffs-ff)/20;
						for(fs=ffs;fs>=ff;fs=fs-dfs)
						{
							xx[i]=e*cos(fs);
							yy[i]=e*sin(fs);
							i++;
						}
					}
					else
					{
						dfs=(ff-ffs)/20;
						for(fs=ffs;fs<=ff;fs=fs+dfs)
						{
							xx[i]=e*cos(fs);
							yy[i]=e*sin(fs);
							i++;
						}
					}
				}				
			} // connecting by an arc curve to fill a gap between adjacent petals (the end)

			ffs=ff;

			xx[i]=(rr+e)*cos(ff);
			yy[i]=(rr+e)*sin(ff);

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

	if(fabs(p-1.)>0.00001) // connecting by an arc curve to fill a gap between adjacent petals (start)
	{
		if(ffs>2*pi)
		{
			ffs=ffs-2*pi;
		}
		
		if(ffs>ff0)
		{
			dfs=(ffs-ff0)/20;
			for(fs=ffs;fs>=ff0;fs=fs-dfs)
			{
				i++;
				xx[i]=e*cos(fs);
				yy[i]=e*sin(fs);
			}
		}
		else
		{
			dfs=(ff0-ffs)/20;
			for(fs=ffs;fs<=ff0;fs=fs+dfs)
			{
				i++;
				xx[i]=e*cos(fs);
				yy[i]=e*sin(fs);
			}
		}
	} // connecting by an arc curve to fill a gap between adjacent petals (the end)

	i++;

	xx[i]=xx[1];
	yy[i]=yy[1];

	imax=i;

// writing the calculated coordinates data of the curve into a textfile
	fp=fopen("flower_e.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



_ returning to the page which opened just before with 'return' of browser _

or

_ returning to the previous page of this site with the following button _


RETURN

updated: 2009.03.15, edited by N. Yamamoto
reviced in Sep. 11, 2012.