diff options
Diffstat (limited to 'Random/mandelbrot.c')
-rw-r--r-- | Random/mandelbrot.c | 55 |
1 files changed, 55 insertions, 0 deletions
diff --git a/Random/mandelbrot.c b/Random/mandelbrot.c new file mode 100644 index 0000000..9f8a796 --- /dev/null +++ b/Random/mandelbrot.c @@ -0,0 +1,55 @@ +#include <stdio.h> +#include <stdlib.h> +#include <complex.h> +#include <math.h> +#define ite 500 + +double map(double x, double p1, double p2, double c1, double c2); +double mandel(double y, double x); + +int main(){ + const int dimx = 800, dimy = 800; + int i, j; + FILE *fp = fopen("out.ppm", "wb"); + + fprintf(fp, "P6\n%d %d\n255\n", dimx, dimy); + int c; + unsigned char color[3]; + unsigned char temp; + for(j=-dimy/2; j<dimy/2; ++j){ + for(i=-dimx/2; i<dimx/2; ++i){ + temp = (int)mandel(map(j, -dimx/2, dimx/2, -2, 2), map(i, -dimx/2, dimx/2, -2, 2)); + color[0] = temp; + color[1] = temp; + color[2] = temp; + fwrite(color, 3, 1, fp); + } + } + + fclose(fp); + return 0; +} + +double mandel(double y, double x){ + int i; + double ca = x, + cb = y; + double zx, zy, tzx, tzy; + zx = x; + zy = y; + for(i=0; i<ite; ++i){ + tzx = zx; + tzy = zy; + zx = tzx*tzx - tzy*tzy + ca; + zy = 2*tzx*tzy + cb; + if(zx*zx + zy*zy > 4) + break; + } + + return map(i, ite, 0, 0, 255); +} + + +double map(double x, double p1, double p2, double c1, double c2){ + return 1.0*(x-p1)*(c2-c1)/(p2-p1) + c1; +}
\ No newline at end of file |