aboutsummaryrefslogtreecommitdiff
path: root/Random/mandelbrot.c
blob: 9f8a796946d40f806551a31b6c73d5f5e49f4a09 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
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;
}