Bonjour
Ayant beaucoup travaillé sur les fractals,je vous présente tout mes résultats.
Vous pouvez proposer d'autres codes ou des amélioration possibles.
D'abord,l'ensemble de Mandelbrot
Voici mon code
Voila celui des julias
Code Mandelbrot : 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 from numpy import * import pygame from pygame.locals import * def pycode(c_1,c_2,c_3): return c_3+c_2*256+c_1*256**2 couleur=zeros((150)) ri,vi,bi=250,0,0 for i in range(25): couleur[i]=pycode(ri,vi,bi) vi+=10 for i in range(25,50): couleur[i]=pycode(ri,vi,bi) ri-=10 for i in range(50,75): couleur[i]=pycode(ri,vi,bi) bi+=10 for i in range(75,100): couleur[i]=pycode(ri,vi,bi) vi-=10 for i in range(100,125): couleur[i]=pycode(ri,vi,bi) ri+=10 for i in range(125,150): couleur[i]=pycode(ri,vi,bi) bi-=10 iteration_max=input("iteration maxi") po=input("point") zoom=float(input("zoom")) eca=zoom/200 x1 = po[0]-1.5/eca x2 = po[0]+1.5/eca y1 = po[1]-1.2/eca y2 = po[1]+1.2/eca image_x = (x2 - x1) * zoom image_y = (y2 - y1) * zoom a=zeros((image_x,image_y),dtype=uint32) for x in range(0,image_x): for y in range(0,image_y): c_r = x / zoom + x1 c_i = y / zoom + y1 z_r = 0 z_i = 0 i = 0 while z_r*z_r + z_i*z_i <4 and i < iteration_max: tmp = z_r z_r = z_r*z_r - z_i*z_i + c_r z_i = 2*z_i*tmp + c_i i = i+1 if i < iteration_max: a[x][y]=couleur[(i*2)%150] M=a hM=len(M) lM=len(M[0]) print hM,lM pygame.init() fenetre = pygame.display.set_mode((hM,lM)) pygame.display.set_caption('mandelbrot') pygame.surfarray.blit_array(fenetre,M) #pygame.image.save(fenetre,"mandelbrot.png") continuer = True while continuer: pygame.time.Clock().tick(1) pygame.display.flip() for event in pygame.event.get(): if event.type == QUIT: continuer = False pygame.quit()
Pour ces deux programmes,l'input("point") attend un tuple avec les deux coordonnées du point
Code julias : 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 from numpy import * import pygame from pygame.locals import * def pycode(c_1,c_2,c_3): return c_3+c_2*256+c_1*256**2 couleur=zeros((150)) ri,vi,bi=250,0,0 for i in range(25): couleur[i]=pycode(ri,vi,bi) vi+=10 for i in range(25,50): couleur[i]=pycode(ri,vi,bi) ri-=10 for i in range(50,75): couleur[i]=pycode(ri,vi,bi) bi+=10 for i in range(75,100): couleur[i]=pycode(ri,vi,bi) vi-=10 for i in range(100,125): couleur[i]=pycode(ri,vi,bi) ri+=10 for i in range(125,150): couleur[i]=pycode(ri,vi,bi) bi-=10 iteration_max=input("iteration maxi") zoom=float(input("zoom")) point=input("point") eca=zoom/200 x1 = -1.5 x2 = 1.5 y1 = -1.2 y2 = 1.2 image_x = (x2 - x1) * zoom image_y = (y2 - y1) * zoom a=zeros((image_x,image_y),dtype=uint32) for x in range(0,image_x): for y in range(0,image_y): c_r =point[0] c_i =point[1] z_r = x / zoom + x1 z_i = y / zoom + y1 i = 0 while z_r*z_r + z_i*z_i <4 and i < iteration_max: tmp = z_r z_r = z_r*z_r - z_i*z_i + c_r z_i = 2*z_i*tmp + c_i i = i+1 if i < iteration_max: a[x][y]=couleur[(i*2)%150] M=a hM=len(M) lM=len(M[0]) print hM,lM pygame.init() fenetre = pygame.display.set_mode((hM,lM)) pygame.display.set_caption('julias') pygame.surfarray.blit_array(fenetre,M) #pygame.image.save(fenetre,"julias.png") continuer = True while continuer: pygame.time.Clock().tick(1) pygame.display.flip() for event in pygame.event.get(): if event.type == QUIT: continuer = False pygame.quit()
Pour vous aider a trouver des point pour les julias,j'ai aussi fait ce programme qui donne les coordonnées du point où l'on a cliqué.
Ensuite,pour la fractal du C et de Koch,j'ai fait ce programme qui dessine la fractal a partir de la figure de base.
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 import pygame from pygame.locals import * from numpy import * def pycode(c_1,c_2,c_3): return c_3+c_2*256+c_1*256**2 couleur=zeros((250)) ri,vi,bi=0,0,0 for i in range(35): couleur[i]=pycode(ri,vi,bi) ri+=7 for i in range(35,71): couleur[i]=pycode(ri,vi,bi) vi+=7 for i in range(71,107): couleur[i]=pycode(ri,vi,bi) ri-=7 for i in range(107,142): couleur[i]=pycode(ri,vi,bi) bi+=7 for i in range(142,178): couleur[i]=pycode(ri,vi,bi) vi-=7 for i in range(178,214): couleur[i]=pycode(ri,vi,bi) ri+=7 for i in range(214,250): couleur[i]=pycode(ri,vi,bi) vi+=7 couleur[249]=0 zoom=200.0 iteration_max=50 x1 = -2.5 x2 = 0.5 y1 = -1.2 y2 = 1.2 image_x = (x2 - x1) * zoom image_y = (y2 - y1) * zoom a=zeros((image_x,image_y),dtype=uint32) pas=250.0/iteration_max for x in range(0,image_x): #print x for y in range(0,image_y): c =complex( x / zoom + x1,y / zoom + y1) z =complex(0.0,0.0) i = 0 while z.real**2+z.imag**2<4 and i < iteration_max: z=z*z+c i = i+1 if i <= iteration_max: a[x][y]=couleur[(i*pas)-1] M=a hM=len(M) lM=len(M[0]) pygame.init() print (hM,lM) fenetre = pygame.display.set_mode((hM,lM)) pygame.surfarray.blit_array(fenetre,M) continuer = True pygame.display.flip() while continuer: for event in pygame.event.get(): if event.type == QUIT: continuer = False if event.type == MOUSEBUTTONDOWN: print str(event.pos[0] / zoom + x1)+","+str(event.pos[1] / zoom + y1) pygame.display.flip() pygame.quit()
exemples:pour la fractal du C,la base sera 0,90
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 from numpy import * import pygame from pygame.locals import * M=(ones((950,550))*16777215) base=list(input("base")) for i in range(len(base)): base[i]=(base[i]/180.0)*pi n=len(base) it=input("iteration") lt=[] liste=base[:] for i in range(it): for j in range(n): for k in range(0,len(liste)): lt.append(liste[k]+base[j]) liste=lt[:] lt=[] x=5 y=5 for i in range(len(liste)): x+=cos(liste[i]) y+=sin(liste[i]) try: M[int(x)][int(y)]=0 except: pass hM=len(M) lM=len(M[0]) print hM,lM pygame.init() fenetre = pygame.display.set_mode((hM,lM)) pygame.display.set_caption('Fractal du C') pygame.surfarray.blit_array(fenetre,M) #pygame.image.save(fenetre,"frac_C.png") continuer = True while continuer: pygame.time.Clock().tick(1) pygame.display.flip() for event in pygame.event.get(): if event.type == QUIT: continuer = False pygame.quit()
pour la fractal de Koch,la base sera 0,60,-60,0
mais on peut en faire bien d'autres
Pour la courbe du dragon,je me suis amusé a la colorer de différentes façon:
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 from numpy import * import pygame from pygame.locals import * def pycode(c_1,c_2,c_3): return c_3+c_2*256+c_1*256**2 couleur=zeros((150)) ri,vi,bi=250,0,0 for i in range(25): couleur[i]=pycode(ri,vi,bi) vi+=10 for i in range(25,50): couleur[i]=pycode(ri,vi,bi) ri-=10 for i in range(50,75): couleur[i]=pycode(ri,vi,bi) bi+=10 for i in range(75,100): couleur[i]=pycode(ri,vi,bi) vi-=10 for i in range(100,125): couleur[i]=pycode(ri,vi,bi) ri+=10 for i in range(125,150): couleur[i]=pycode(ri,vi,bi) bi-=10 M=(ones((950,550))*16777215) lx=[1] ly=[1] lc=[1] it=input("iteration") for k in range(it): ltc=lc[:] for i in range(0,len(ltc)): lc.append(1+ltc[len(ltc)-i-1]) for k in range(it): ltx=lx[:] lty=ly[:] for i in range(0,len(ltx)): ly.append(ltx[len(ltx)-i-1]) for i in range(0,len(lty)): lx.append(-lty[len(lty)-i-1]) x=400 y=275 for i in range(len(lx)): x=x+lx[i] try: M[int(x)][int(y)]=couleur[(lc[i]*9)%250] except: pass y=y+ly[i] try: M[int(x)][int(y)]=couleur[(lc[i]*9)%250] except: pass hM=len(M) lM=len(M[0]) print hM,lM pygame.init() fenetre = pygame.display.set_mode((hM,lM)) pygame.display.set_caption('dragon') pygame.surfarray.blit_array(fenetre,M) continuer = True while continuer: pygame.time.Clock().tick(1) pygame.display.flip() for event in pygame.event.get(): #Attente des événements if event.type == QUIT: continuer = False pygame.quit()finalement c'est quasiment la même chose(mais pas exactement)
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 from numpy import * import pygame from pygame.locals import * def pycode(c_1,c_2,c_3): return c_3+c_2*256+c_1*256**2 couleur=zeros((150)) ri,vi,bi=250,0,0 for i in range(25): couleur[i]=pycode(ri,vi,bi) vi+=10 for i in range(25,50): couleur[i]=pycode(ri,vi,bi) ri-=10 for i in range(50,75): couleur[i]=pycode(ri,vi,bi) bi+=10 for i in range(75,100): couleur[i]=pycode(ri,vi,bi) vi-=10 for i in range(100,125): couleur[i]=pycode(ri,vi,bi) ri+=10 for i in range(125,150): couleur[i]=pycode(ri,vi,bi) bi-=10 M=(ones((950,550))*16777215) lx=[1] ly=[1] it=input("iteration") for k in range(it): ltx=lx[:] lty=ly[:] for i in range(0,len(ltx)): ly.append(ltx[len(ltx)-i-1]) for i in range(0,len(lty)): lx.append(-lty[len(lty)-i-1]) x=400 y=275 co=[0] for i in range(0,len(lx)-1): co.append(co[len(co)-1]+(lx[i]*-ly[i])*-10) co.append(co[len(co)-1]+(ly[i]*lx[i+1])*-10) print co for i in range(len(lx)): x=x+lx[i] try: M[int(x)][int(y)]=couleur[co[i]%150] except: pass y=y+ly[i] try: M[int(x)][int(y)]=couleur[co[i]%150] except: pass hM=len(M) lM=len(M[0]) print hM,lM pygame.init() fenetre = pygame.display.set_mode((hM,lM)) pygame.display.set_caption('Fractal du C') pygame.surfarray.blit_array(fenetre,M) continuer = True while continuer: pygame.time.Clock().tick(1) pygame.display.flip() for event in pygame.event.get(): if event.type == QUIT: continuer = False pygame.quit()
Sinon,il y a celui là qui est plus classique:
pour le triangle de Sierpiński,j'ai trouvé celui là qui marche bien ,sur internet dans un forum.
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 from numpy import * import pygame from pygame.locals import * def pycode(c_1,c_2,c_3): return c_3+c_2*256+c_1*256**2 couleur=zeros((150)) ri,vi,bi=250,0,0 for i in range(25): couleur[i]=pycode(ri,vi,bi) vi+=10 for i in range(25,50): couleur[i]=pycode(ri,vi,bi) ri-=10 for i in range(50,75): couleur[i]=pycode(ri,vi,bi) bi+=10 for i in range(75,100): couleur[i]=pycode(ri,vi,bi) vi-=10 for i in range(100,125): couleur[i]=pycode(ri,vi,bi) ri+=10 for i in range(125,150): couleur[i]=pycode(ri,vi,bi) bi-=10 M=(ones((950,550))*16777215) lx=[1] ly=[1] lc=[1] it=input("iteration") for k in range(it): ltx=lx[:] lty=ly[:] for i in range(0,len(ltx)): ly.append(ltx[len(ltx)-i-1]) for i in range(0,len(lty)): lx.append(-lty[len(lty)-i-1]) x=400 y=275 co=0 for i in range(len(lx)): co+=0.0025 x=x+lx[i] try: M[int(x)][int(y)]=couleur[co%150] except: pass y=y+ly[i] try: M[int(x)][int(y)]=couleur[co%150] except: pass hM=len(M) lM=len(M[0]) print hM,lM pygame.init() fenetre = pygame.display.set_mode((hM,lM)) pygame.display.set_caption('dragon') pygame.surfarray.blit_array(fenetre,M) continuer = True while continuer: pygame.time.Clock().tick(1) pygame.display.flip() for event in pygame.event.get(): if event.type == QUIT: continuer = False pygame.quit()
J'en ai aussi fait un avec une autre technique
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 from Tkinter import * def triangle(canevas,coord=((0,200),(100,0),(200,200)),color='#000000'): canevas.create_polygon(coord[0][0],coord[0][1],coord[1][0],coord[1][1],coord[2][0],coord[2][1],fill=color) def genereTriangle(canevas,larg=200,iteration=5): larg=float(larg) canevas.configure(height=larg,width=larg,bg='#00FF00') triangle(canevas,((0,larg),(larg/2,0),(larg,larg)),color='#FF0000') liste_1=[((larg/4,larg/2),(larg*3/4,larg/2),(larg/2,larg))] i=0 larg=larg/2 while i<iteration: liste_2=[] for coord in liste_1: triangle(canevas,coord) x,y=coord[0][0],coord[0][1] haut=coord[2][1]-coord[0][1] quart=larg/4 #definition des trois nouveau triangles liste_2.append(((x+quart,y-(haut/2)),(x+(3*quart),y-(haut/2)),(x+(2*quart),y)))#triangle haut liste_2.append(((x-quart,y+(haut/2)),(x+quart,y+(haut/2)),(x,y+haut)))#triangle bas gauche liste_2.append(((x+(3*quart),y+(haut/2)),(x+(5*quart),y+(haut/2)),(x+(4*quart),y+haut)))#triangle bas droit larg=larg/2 liste_1=liste_2[:] i+=1 taille=int(input('Entrez la largeur du triangle de sierpinski:')) iter=int(input('Entrez le nombre d\'iteration:')) fen=Tk() can=Canvas(fen) can.pack() genereTriangle(can,taille,iter) fen.mainloop()
Sinon,pour mandelbrot j'ai fait ce code la en C++ pour plus de performances
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 from numpy import * import pygame from pygame.locals import * def test(x,y): if x==y: return 1 else: return 0 it=2**input("iterations") M=ones((it+1,it+1)) M[0][1]=0 for a in range(it): for b in range(1,a+1): M[b][a+1]=test(M[b-1][a],M[b][a]) M*=16777215 hM=len(M) lM=len(M[0]) print hM,lM pygame.init() fenetre = pygame.display.set_mode((hM,lM)) pygame.display.set_caption('sierpinski') pygame.surfarray.blit_array(fenetre,M) continuer = True while continuer: pygame.time.Clock().tick(1) pygame.display.flip() for event in pygame.event.get(): if event.type == QUIT: continuer = False pygame.quit()
Voila,c'est tout.
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
190
191
192
193
194
195
196
197
198
199
200
201
202 #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; unsigned int tab[600][480]; 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) tab[x][y]=couleur[i%150]; } } SDL_EnableKeyRepeat(3, 3); while (continuer) { SDL_PollEvent(&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_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) tab[x][y]=couleur[i%150]; else tab[x][y]=0; } } } tempsActuel = SDL_GetTicks(); if (tempsActuel - tempsPrecedent > 30) { tempsPrecedent = tempsActuel; for (int i = 0; i < 600; i++) { for (int j = 0; j < 480;j++) { definirPixel(ecran,i,j,tab[i][j]); } } } else { SDL_Delay(30 - (tempsActuel - tempsPrecedent)); } SDL_Flip(ecran); } SDL_Quit(); return EXIT_SUCCESS; }
N'hésitez pas à demander des explication.
Partager