#include #include #include #include #include /* buddhabrot.c Blake Shaw - 11/08 Adapted from code from Paul Bourke To compile: gcc -Wall buddhabrot.c -o buddhabrot Example usage: ./buddhabrot 500 1000 100 buddha */ #define TRUE 1 #define FALSE 0 #define MAX(x,y) (x > y ? x : y) #define MIN(x,y) (x < y ? x : y) typedef struct { double x,y; } XY; // Prototypes void WriteImage(char *,unsigned int *,int,int); void WriteData(char *,unsigned int *,int,int); int Iterate(int,double,double,int *,XY *); void sigproc(); int doASAP; int main(int argc,char **argv) { int i,t,tt,n,ix,iy,NX,NY,NMAX,TMAX; char imageFile[100]; char dataFile[100]; int particleCount = 0; double x,y; XY *xyseq = NULL; signal(SIGINT, sigproc); // Parse command line arguments if (argc == 5) { NX = atoi(argv[1]); //Resolution X NY = atoi(argv[1]); //Resolution Y NMAX = atoi(argv[2]); //Bailout TMAX = atoi(argv[3]); //Num iterations in millions printf("Size: %dx%d, Bailout: %d, File: %s\n", NX, NY, NMAX, argv[4]); } else { printf("./buddha res bailout numIts fileName\n"); return(0); } sprintf(imageFile, "%s-%d-%d-%d.tga", argv[4], NX, NMAX, TMAX); sprintf(dataFile, "%s-%d-%d-%d.dat", argv[4], NX, NMAX, TMAX); // 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); } // Initialize for (i=0;i= 0 && iy >= 0 && ix < NX && iy < NY) { image[iy*NX+ix]++; } } particleCount++; } } printf("%d million iterations -- %d particles\n",t+1, particleCount); } // Save the result WriteImage(imageFile,image,NX,NY); WriteData(dataFile,image,NX,NY); printf("Size: %dx%d, Bailout: %d, File: %s\n", NX, NY, NMAX, argv[4]); exit(0); } /* Write the buddha image to a data file. Triplets for sparse matrix in matlab */ void WriteData(char *fname,unsigned int *image,int width,int height) { int i; FILE *fptr; // Write the file fprintf(stderr,"Writing \"%s\"\n",fname); if ((fptr = fopen(fname,"w+")) == NULL) { fprintf(stderr,"Failed to open output file\n"); exit(0); } 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(int NMAX,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); } void sigproc() { char inputc; printf("\nEnter command\n"); printf("(w)rite image, (q)uit and write: "); if (scanf("%c", &inputc) == 1){ switch (inputc) { case 'w': doASAP = 1; break; case 'q': doASAP = 2; break; default: doASAP = 0; } } }