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


#include<stdio.h>	

/* definition of constants */

#define Radius 10	
#define Side 2.0		
#define Cx 0.561321		
#define Cy 0.641	
#define M 700	
#define Num 255	
#define height Num/4	

/* main program */
main()
{

	int p, q, n, w, r[M+height];	
	double x, y, xx, yy, Incx, Incy;	

	FILE *fp;
	fp = fopen("Julia.raw","w");	

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

		for (q = 1; q <= M; q++)
		{
			Incx = - Side + 4*Side*(p+q/2)/M/3;
			Incy = - Side + 2*Side*q/M;
			
		x = Incx;
		y = Incy;
		w = 100;		

			for ( n = 1; n <= Num; ++n)
			{
				xx = x*x - y*y - Cx;
				yy = 2*x*y - Cy;
				
				x = xx;
				y = yy;
				
				if ( x*x + y*y > Radius )	
					{
						w = n;
						n = Num;
					}
			}
				
						r[q + height/w] = w;
				
		}

		
		for ( q = 1; q <= M; q++)
			{	
			
		fprintf(fp, "%c", 1+r[q] );		
		
		r[q] = 100;			
	   }
	}
	
	fclose(fp);							
}

/* end of main program */