Bonjour
Étant passionné par les fractales(notamment l'ensemble de Mandelbrot),j'ai créé un code qui permet de zoomer dessus et de sauvegarder des images de zoom.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
#include <SDL/SDL.h>
int pycode(int c_1,int c_2,int c_3)
{
    return c_3+c_2*256+c_1*256*256;
}
void definirPixel(SDL_Surface *surface, int x, int y, Uint32 pixel)
{
    int nbOctetsParPixel = surface->format->BytesPerPixel;
    Uint8 *p = (Uint8 *)surface->pixels + y * surface->pitch + x * nbOctetsParPixel;
    switch(nbOctetsParPixel)
    {
        case 1:
            *p = pixel;
            break;
        case 2:
            *(Uint16 *)p = pixel;
            break;
        case 3:
            if(SDL_BYTEORDER == SDL_BIG_ENDIAN)
            {
                p[0] = (pixel >> 16) & 0xff;
                p[1] = (pixel >> 8) & 0xff;
                p[2] = pixel & 0xff;
            }
            else
            {
                p[0] = pixel & 0xff;
                p[1] = (pixel >> 8) & 0xff;
                p[2] = (pixel >> 16) & 0xff;
            }
            break;
        case 4:
            *(Uint32 *)p = pixel;
            break;
    }
}
int main(int argc, char *argv[])
{
    int ri=250;
    int vi=0;
    int bi=0;
    int i;
    int couleur[150];
    for (i=0;i<25;i++)
    {
        couleur[i]=pycode(ri,vi,bi);
        vi+=10;
    }
    for (;i<50;i++)
    {
        couleur[i]=pycode(ri,vi,bi);
        ri-=10;
    }
    for (;i<75;i++)
    {
        couleur[i]=pycode(ri,vi,bi);
        bi+=10;
    }
    for (;i<100;i++)
    {
        couleur[i]=pycode(ri,vi,bi);
        vi-=10;
    }
    for (;i<125;i++)
    {
        couleur[i]=pycode(ri,vi,bi);
        ri+=10;
    }
    for (;i<150;i++)
    {
        couleur[i]=pycode(ri,vi,bi);
        bi-=10;
    }
    double xa=0,ya=0;
    SDL_Surface *ecran = NULL;
    SDL_Event event;
    int continuer = 1;
    int tempsPrecedent = 0, tempsActuel = 0;
    SDL_Init(SDL_INIT_VIDEO);
    ecran = SDL_SetVideoMode(600, 480, 32, SDL_HWSURFACE | SDL_DOUBLEBUF);
    SDL_WM_SetCaption("Mandelbrot", NULL);
    int iteration_max=150;
    double zoom=200;
    double eca=zoom/200;
    double x1 =-1.5/eca;
    double x2 =1.5/eca;
    double y1 =-1.2/eca;
    double y2 =1.2/eca;
    int image_x = (x2 - x1) * zoom;
    int image_y = (y2 - y1) * zoom;
    for(int x=0;x<image_x;x++)
    {
        for(int y=0;y<image_y;y++)
        {
            double c_r = x / zoom + x1;
            double c_i = y / zoom + y1;
            double z_r = 0;
            double z_i = 0;
            int i = 0;
            while(z_r*z_r + z_i*z_i <4 && i < iteration_max)
            {
                double tmp = z_r;
                z_r = z_r*z_r - z_i*z_i + c_r;
                z_i = 2*z_i*tmp + c_i;
                i++;
            }
            if (i < iteration_max)
                definirPixel(ecran,x,y,couleur[i%150]);
            else
                definirPixel(ecran,x,y,0);
        }
    }
    while (continuer)
    {
        SDL_WaitEvent(&event);
        switch(event.type)
        {
            case SDL_QUIT:
                continuer = 0;
                break;
            case SDL_KEYDOWN:
                switch (event.key.keysym.sym)
                {
                    case SDLK_u:
                        iteration_max+=50;
                        break;
                    case SDLK_t:
                        SDL_SaveBMP(ecran,"mandelbrot.bmp");
                        break;
                    case SDLK_RALT:
                        zoom/=1.5;
                        break;
                    case SDLK_r:
                        zoom=200;
                        break;
                    case SDLK_LEFT:
                        xa-=50/zoom;
                        break;
                    case SDLK_RIGHT:
                        xa+=50/zoom;
                        break;
                    case SDLK_UP:
                        ya-=50/zoom;
                        break;
                    case SDLK_DOWN:
                        ya+=50/zoom;
                        break;
                    case SDLK_SPACE:
                        zoom*=1.5;
                        break;
                    case SDLK_BACKSPACE:
                        iteration_max+=20;
                        break;
                    case SDLK_e:
                        iteration_max-=50;
                        break;
                }
                long double eca=zoom/200;
                long double x1 =xa-1.5/eca;
                long double x2 =xa+1.5/eca;
                long double y1 =ya-1.2/eca;
                long double y2 =ya+1.2/eca;
                int image_x = (x2 - x1) * zoom;
                int image_y = (y2 - y1) * zoom;
                for(int x=0;x<image_x;x++)
                {
                    for(int y=0;y<image_y;y++)
                    {
                        long double c_r = x / zoom + x1,c_i = y / zoom + y1,z_r = 0,z_i = 0;
                        int i = 0;
                        while(z_r*z_r + z_i*z_i <4 && i < iteration_max)
                        {
                            long double tmp = z_r;
                            z_r = z_r*z_r - z_i*z_i + c_r;
                            z_i = 2*z_i*tmp + c_i;
                            i++;
                        }
                        if (i < iteration_max)
                            definirPixel(ecran,x,y,couleur[i%150]);
                        else
                            definirPixel(ecran,x,y,0);
                    }
                }
        }
        SDL_Flip(ecran);
    }
    SDL_Quit();
    return EXIT_SUCCESS;
}
Il marche plutôt bien mais calculer cette fractale étant toujours assez lent,j'aimerais que l'on propose des optimisations ou des amélioration.
N’hésitez pas à poser des question dessus.