int mandlebrot(complex c, int i) { complex tmp, z; z = c; while(i --> 0) { //square z & store in tmp, add c and store in z complexmult(&tmp, z, z); complexadd(&z, tmp, c); } float ret = complexabs(z); if(!isfinite(ret)) ret = 0xff; return ret >= 2.0F ? 0 : 1; }
int main(int argc, char** argv) { int grid_size_x; int grid_size_y; int max_iter; float xmin; float xmax; float ymin; float ymax; int **image; int i, j, n; mycomplex c, z, ztmp; double xscale, yscale; read_options(argc, argv, &grid_size_x, &grid_size_y, &max_iter, &xmin, &xmax, &ymin, &ymax); initialise_image(&image, grid_size_x, grid_size_y); /* Compute the mandelbrot set here and write results into image * array. Note that the image writing code assumes the following * mapping from array entries to pixel positions in the image: * * A == image[0][0] * B == image[grid_size_y-1][0] * C == image[grid_size_y-1][grid_size_x-1] * D == image[0][grid_size_x-1] * * B-----------------C * | | * | | * A-----------------D */ xscale = (xmax - xmin)/grid_size_x; yscale = (ymax - ymin)/grid_size_y; for(i=0; i<grid_size_y; ++i) { for(j=0; j<grid_size_x; ++j) { c.x = xmin + j*xscale; c.y = ymin + i*yscale; z.x = c.x; z.y = c.y; n = 0; while(complexabs(z)<=2) { ztmp = complexsquared(z); z.x = ztmp.x + c.x; z.y = ztmp.y + c.y; n++; if(n == max_iter) { break; } } image[i][j] = n; } } write_ppm("output.ppm", image, grid_size_x, grid_size_y, max_iter); free(image); return 0; }