sequential mandelbrot image creation
Posted
by
visanio_learner
on Stack Overflow
See other posts from Stack Overflow
or by visanio_learner
Published on 2012-03-21T13:16:57Z
Indexed on
2012/03/22
11:29 UTC
Read the original article
Hit count: 286
can anyone tell me what is wrong in this code?
i am getting an error in the main function, when i am calling the 'file_write' method function inside the main function, it says 'y' is not declared in this scope, but actually it was an argument that i have passed it in the method function earlier.
is it a serious error?
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
unsigned int width = 1500;
unsigned int height = 1500;
unsigned int max_iterations = 30000;
unsigned int **color = NULL;
double threshold = 4;
double min_re = -2.0;
double max_re = 1.0;
double min_im = -1.2;
double max_im = min_im+(max_re-min_re)*height/width;
double x_factor = (max_re-min_re)/(width-1);
double y_factor = (max_im-min_im)/(height-1);
int file_write(int x, int y, double min_re, double max_im, double x_factor, double y_factor)
{
FILE *fractal = fopen("mandelbrot_imageSequential.ppm","w+");
if(fractal != NULL)
{
fprintf(fractal,"P6\n");
fprintf(fractal,"# %s\n", "Mandelbrot_imageSequential.ppm");
fprintf(fractal,"%d %d\n", height, width);
fprintf(fractal,"255\n");
unsigned int R = 0, G = 0, B = 0;
for(unsigned y = 0; y < height; ++y)
{
double c_im = max_im - y*y_factor;
for(unsigned x = 0; x < width; ++x)
{
double c_re = min_re + x*x_factor;
double Z_re = c_re, Z_im = c_im;
bool image_inside = true;
R = 0, G = 0, B = 0;
for(unsigned n=0; n<max_iterations; ++n)
{
double Z_re2 = Z_re*Z_re, Z_im2 = Z_im*Z_im;
if(Z_re2 + Z_im2 > threshold)
{
image_inside = false;
G = n;
if(G == 10)
{
G = 25, R = 10, B = 5;
}
break;
}
Z_im = 2 * Z_re * Z_im + c_im;
Z_re = Z_re2 - Z_im2 + c_re;
}
if(image_inside)
{
putc(R, fractal);
putc(G, fractal);
putc(B, fractal);
}
else
{
putc(R, fractal);
putc(G, fractal);
putc(B, fractal);
}
}
}
fclose(fractal);
return 0;
}
}
int main(int argc, char *argv[])
{
if(argc != 9)
{
printf("There is an error in the input given.\n");
return 0;
}
else
{
height = atoi(argv[1]);
width = atoi(argv[2]);
max_iterations = atoi(argv[3]);
min_re = atof(argv[4]);
max_re = atof(argv[5]);
min_im = atof(argv[6]);
max_im = atof(argv[7]);
threshold = atoi(argv[8]);
}
color = (unsigned int**)malloc(height*sizeof(unsigned int*));
printf("height = %d\twidth = %d\tmaximum_iterations = %d\tminimum_x-value = %.2f\tmaximum_x-value = %.2f\tminimum_y-value = %.2f\tmaximum_y-value = %.2f\tthreshold_value = %.2f\t\n",height,width,max_iterations,min_re,max_re,min_im,max_im,threshold);
int x;
for(x = 0; x < height; x++)
{
color[x] = (unsigned int*)malloc(width*sizeof(unsigned int));
}
time_t ts,te;
time(&ts);
file_write(x,y,min_re,max_im,x_factor,y_factor);
time(&te);
double diff = difftime(te,ts);
printf("Total Time elapsed: %f\n",diff);
for(x = 0; x < height; x++)
{
free(color[x]);
}
free(color);
return 0;
}
© Stack Overflow or respective owner