/***********************************************
* Mnad3Dfire.c
*
*  Generates a RAW format file of a 3D image 
*  of Mandelbrot set (grayscale)
*
*  C source file by Alberto Strumia
*
************************************************/


#include<stdio.h>

/* definition of constants */

#define Radius 10
#define Side 0.75
#define Cx -1.5*Side
#define Cy 0.8*Side
#define scale 2
#define M 700
#define Num 255
#define co 1.41
#define si 1.41


/* main program */
main()
{

	int p, q, n, w, ind, r[M];
	double x, y, xx, yy, Incx, Incy;
	
	FILE *fp;
	fp = fopen("Mand.raw","w");
	
	fprintf(fp, "%d %d\n", M, M);

	for (p = 1; p <= M; p++)
	{
		
			printf("%i %%\n", p*100/M);

		
		for (q = 1; q <= M; q++)
		{
			Incx = Cx - Side + 2*Side*(p*co+q*si)/M;
			Incy = Cy - Side + 2*Side*(-p*si+q*co)/M;
			
		x = 0;
		y = 0;
		w = 0;						/* background color 1 */
		
			for ( n = 1; n <= Num; ++n)
			{
				xx = x*x - y*y - Incx;
				yy = 2*x*y - Incy;
				
				x = xx;
				y = yy;
				
				if ( x*x + y*y > Radius )
					{
						w = n;
						n = Num;
					}
			 }
			
				if ( q >= w )
					{
						r[q - w/scale] = w;
					}
		}
				
		for ( q = 1; q <= M; q++)
			{	
			
		fprintf(fp, "%c", r[q] );         	/* fire effect */
		
		r[q] = 0;	 							/* background color  2 */
			}
	}
	
	fclose(fp);
}

/* end of main program */