/* Bryan Ward buddha.c - buddhabrot generation program */ #include #include #include #include #define width 1280 #define height 1024 #define iter 1000 #define iteration (10000000L) #define xmin -2.0 #define xmax 2.0 #define ymin -2.0 #define ymax 2.0 #define max(x,y) (x > y ? x : y) #define min(x,y) (x < y ? x : y) int checkPoint(double complex c, int *n, double complex *seq) { int k; double complex z = 0; double complex new; *n = 0; for(k = 0;k < iter;k++) { z = z*z + c; seq[k] = z; if(creal(z)*creal(z) + cimag(z) *cimag(z) > 10) { *n = k; return 1; } } return 0; } int main(int argc, char ** argv) { FILE* fl; int i,n,ix,iy,t; double complex c; unsigned int *image = NULL; double complex *seq = NULL; int max,min,range; if((image = (unsigned int*)malloc(width*height*sizeof(unsigned int))) == NULL) { fprintf(stderr,"Failed to malloc memory for the image\n"); } if((seq = (double complex*)malloc(iter*sizeof(unsigned int))) == NULL) { fprintf(stderr,"Failed to malloc memory for the sequence\n"); } for(i = 0; i < width*height;i++) { image[i] = 0; } for(t = 0;t < iteration;t++) { c = 6*drand48() - 3 + (6*drand48()-3)*I; if(checkPoint(c,&n,seq)) { for(i = 0; i < n;i++) { ix = .3 * width * (creal(seq[i]) + .5) + width/2; iy = .3 * height * cimag(seq[i]) + height/2; if(ix >=0 && iy >= 0 && ix < width && iy < height) image[iy*width+ix]++; } } } if((fl=fopen("image_buddha.pgm","w"))==NULL) { fprintf(stderr,"Failed to open the image file"); } fprintf(fl,"P5\n#Copyright (C) 2007 Bryan Ward\n%d\t%d\n255\n",width,height); max = image[0]; min = image[0]; for(i = 0; i < width*height;i++) { max = max(max,image[i]); min = min(min,image[i]); } printf("Max: %d\tMin: %d\n",max,min); range = max - min; for(i = 0; i < width*height;i++) { fprintf(fl,"%c",(unsigned char)(255*(image[i]-min)/range)); } free(seq); free(image); close(fl); return 0; }