#include #include #include /* Crude, but functional, little program to calculate the So called "budda mandelbrot" or "buddhabrot" Please note that this is not supposed to be the most efficient implementation, it is only intended to be a simple example with plenty of scope for improvement by the reader. */ #define TRUE 1 #define FALSE 0 #define MAX(x,y) (x > y ? x : y) #define MIN(x,y) (x < y ? x : y) // Image dimensions #define NX 1000 #define NY 1000 // Length of sequece to test escape status // Also known as bailout #define NMAX 200 // Number of iterations, multiples of 1 million #define TMAX (1000L) typedef struct { double x,y; } XY; // Prototypes void WriteImage(char *,unsigned int *,int,int); int Iterate(double,double,int *,XY *); int main(int argc,char **argv) { int i,n,ix,iy; long t,tt; double x,y; XY *xyseq = NULL; // The density plot unsigned int *image = NULL; // Malloc space for the image and clear to black if ((image = (unsigned int *)malloc(NX*NY*sizeof(unsigned int))) == NULL) { fprintf(stderr,"Failed to malloc memory for the image\n"); exit(-1); } for (i=0;i= 0 && iy >= 0 && ix < NX && iy < NY) image[iy*NX+ix]++; } } } / t } // tt // Save the result WriteImage("buddha_single.tga",image,NX,NY); exit(0); } /* Write the buddha image to a minimal TGA file. Can be opened with gimp, PhotoShop, etc. */ void WriteImage(char *fname,unsigned int *image,int width,int height) { int i; float ramp,biggest=0,smallest; FILE *fptr; // Find the largest density value for (i=0;i 1) ramp = 1; ramp = pow(ramp,0.5); fputc((int)(ramp*255),fptr); fputc((int)(ramp*255),fptr); fputc((int)(ramp*255),fptr); } fclose(fptr); } /* Iterate the Mandelbrot and return TRUE if the point escapes */ int Iterate(double x0,double y0,int *n,XY *seq) { int i; double x=0,y=0,xnew,ynew; *n = 0; for (i=0;i 10) { *n = i; return(TRUE); } x = xnew; y = ynew; } return(FALSE); }