suite de mon approche du monde des fractales. Aujourd'hui : le Chaos Game (j'espère ne pas dire trop de bêtise )
Le code permettant de générer les formes :
Où form est une structure avec les champs suivant
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 function [x,y,c] = chaosgame(form,N) %CHAOSGAME % % % Author : Jerome Briot (Dut) % Contact : dutmatlab#yahoo#fr -or- briot#cict#fr % Profil : www.mathworks.com/matlabcentral/newsreader/author/94805 % : www.developpez.net/forums/u125006/dut/ % % Version : 1.0 - 03 Sep 2009 % % MATLAB : 7.6.0.324 (R2008a) % System : Linux 2.6.24-24-generic % error(nargchk(1,2,nargin)); if nargin == 1 N = 75000; end m = min(form.polyg,[],2); M = max(form.polyg,[],2); [x,y] = meshgrid(linspace(m(1),M(1),10),linspace(m(2),M(2),10)); in = inpolygon(x(:),y(:),form.polyg(1,:),form.polyg(2,:)); idxi = find(in(:)); idx = randperm(numel(idxi)); idxi = idxi(idx(1)); xs = x(idxi(1)); ys = y(idxi(1)); sp = size(form.polyg,2); idx = randperm(sp); x = zeros(1,N); y = zeros(1,N); c = zeros(1,N); if form.keeppolyg x(1:sp) = form.polyg(1,:); y(1:sp) = form.polyg(2,:); c(1:sp) = 1:sp; k = sp+1; else k = 1; end x(k) = form.frac*form.polyg(1,idx(1))+(1-form.frac)*xs; y(k) = form.frac*form.polyg(2,idx(1))+(1-form.frac)*ys; c(k) = idx(1); for n=k+1:N idx = randperm(sp); x(n) = form.frac*form.polyg(1,idx(1))+(1-form.frac)*x(n-1); y(n) = form.frac*form.polyg(2,idx(1))+(1-form.frac)*y(n-1); c(n) = idx(1); end
- form.name
- form.polyg
- form.frac
- form.keeppolyg
En prenant l'exemple du Triangle de Sierpinski (voir le paragraphe qui commence par "Or more simply:"), la structure form est :
- form.name = 'Sierpinski triangle';
- form.polyg = [0 1 .5 ; 0 0 sqrt(2)/2];
- form.frac = .5;
- form.keeppolyg = true;
Le champs keeppolyg sert juste à spécifier si les points du polygone de départ doivent être conservés ou non pour le tracé final.
N est bien entendu le nombre d'itération donc le nombre de points au final sur le tracé.
Pour tracer le résultat :
Quelques formes prédéfinies :
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 function h = drawchaosgame(x,y,c) %DRAWCHAOSGAME % % % Author : Jerome Briot (Dut) % Contact : dutmatlab#yahoo#fr -or- briot#cict#fr % Profil : www.mathworks.com/matlabcentral/newsreader/author/94805 % : www.developpez.net/forums/u125006/dut/ % % Version : 1.0 - 03 Sep 2009 % % MATLAB : 7.6.0.324 (R2008a) % System : Linux 2.6.24-24-generic % error(nargchk(2,3,nargin)); figure('color','w') if nargin == 3 mc = max(c); hold on cmap = hsv(mc); cmap = cmap(randperm(mc),:); for n = 1:mc idx = c==n; h(n) = plot(x(idx),y(idx),'k.'); set(h(n),'color',cmap(n,:)); end else h = plot(x,y,'k.'); end axis equal off
et le fichier démo :
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 function form = selectchaosgameform(n) %SELECTCHAOSGAMEFORM % % % Author : Jerome Briot (Dut) % Contact : dutmatlab#yahoo#fr -or- briot#cict#fr % Profil : www.mathworks.com/matlabcentral/newsreader/author/94805 % : www.developpez.net/forums/u125006/dut/ % % Version : 1.0 - 03 Sep 2009 % % MATLAB : 7.6.0.324 (R2008a) % System : Linux 2.6.24-24-generic % error(nargchk(1,1,nargin)); fa = {'Sierpinski carpet' 'Sierpinski triangle' 'Sierpinski Hexagon' 'Sierpinski Pentagon' 'Sierpinski Pentagon 2' 'Square' 'Square 2'}; fo = fa{n}; switch fo case 'Sierpinski carpet' form.name = 'Sierpinski carpet'; form.polyg = [0 .5 1 1 1 .5 0 0 ; 0 0 0 .5 1 1 1 .5]; form.frac = 2/3; form.keeppolyg = false; case 'Sierpinski triangle' form.name = 'Sierpinski triangle'; form.polyg = [0 1 .5 ; 0 0 sqrt(2)/2]; form.frac = .5; form.keeppolyg = true; case 'Sierpinski Hexagon' form.name = 'Sierpinski Hexagon'; t = linspace(0,2*pi,7); form.polyg = [sin(t(2:end)); cos(t(2:end))]; form.frac = 2/3; form.keeppolyg = false; case 'Sierpinski Pentagon' form.name = 'Sierpinski Pentagon'; t = linspace(0,2*pi,6); form.polyg = [sin(t(2:end)); cos(t(2:end))]; form.frac = 2/3; form.keeppolyg = false; case 'Sierpinski Pentagon 2' form.name = 'Sierpinski Pentagon 2'; t = linspace(0,2*pi,6); form.polyg = [sin(t(2:end)); cos(t(2:end))]; form.frac = 5/8; form.keeppolyg = false; case 'Square' form.name = 'Square'; form.polyg = [.5 1 .5 0 ; 0 .5 1 .5]*sqrt(2); form.frac = 3/4; form.keeppolyg = true; case 'Square 2' form.name = 'Square 2'; form.polyg = [.5 1 .5 0 ; 0 .5 1 .5]*sqrt(2); form.frac = 3/5; form.keeppolyg = true; end
Voila... si vous avez des remarques, des questions ou des suggestions, n'hésitez pas
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 function demochaosgame %DEMOCHAOSGAME % % % Author : Jerome Briot (Dut) % Contact : dutmatlab#yahoo#fr -or- briot#cict#fr % Profil : www.mathworks.com/matlabcentral/newsreader/author/94805 % : www.developpez.net/forums/u125006/dut/ % % Version : 1.0 - 03 Sep 2009 % % MATLAB : 7.6.0.324 (R2008a) % System : Linux 2.6.24-24-generic % form = selectchaosgameform(2); N = 100000; [x,y,c] = chaosgame(form,N); drawchaosgame(x,y,c); title(sprintf('%s (%d iterations',form.name,N))
Mais rappelez-vous que je ne suis pas un spécialiste dans ce domaine
Note : Ne sachant pas si ces codes sont justes, je ne les ai pas optimisés (et donc pas commentés)
Partager