1 #include <DrawingWindow.h>
2 #include <QApplication>
5 // Fonction de dessin de l'ensemble de Madelbrot, dans la zone
6 // spécifiée, et avec la précision souhgaitée.
7 void do_mandel(DrawingWindow &w,
8 double Rmin, double Rmax, double Imin, double Imax,
11 int x, y; // le pixel considéré
12 double cr, ci; // le complexe correspondant
13 double zr, zi; // pour calculer la suite
14 double zr2, zi2; // zr^2 et zi^2
15 double pr, pi; // taille d'un pixel
16 double rouge, vert, bleu;
19 pr = (Rmax - Rmin) / (w.width - 1);
20 pi = (Imax - Imin) / (w.height - 1);
23 for (x = 0; x < w.width; x++) {
25 for (y = 0; y < w.height; y++) {
29 for (i = 0; zr2 + zi2 < 4 && i < maxiter; i++) {
30 // on calcule le z suivant
31 zi = 2 * zr * zi + ci;
37 rouge = vert = bleu = 0.0;
39 // on est sorti trop tôt du for(...): on affiche le
40 // pixel d'un couleur en fonction de i
41 int ii = (maxiter - 1 - i) % 96;
49 rouge = (ii - 32) / 32.0;
54 vert = (ii - 64) / 32.0;
59 w.setColor(rouge, vert, bleu);
68 // Fonction de dessin principale, calcule la zone d'intérêt, appelle
69 // do_mandel(), pour dessiner l'ensemle, et permet le zoom.
70 void mandel(DrawingWindow &w)
72 // nombre max d'itérations
74 // zone d'intérêt par défaut
84 do_mandel(w, Rmin, Rmax, Imin, Imax, maxiter);
87 w.drawText(5, 5, "Cliquer sur l'image pour zoomer");
91 w.waitMousePress(x, y, button);
93 // calcul des coordonnées du point cliqué
94 Tr = Rmin + x * (Rmax - Rmin) / (w.width - 1);
95 Ti = Imax - y * (Imax - Imin) / (w.height - 1);
97 // calcul de la nouvelle zone d'intérêt :
98 // zoom ×2 en direction du point cliqué
100 double Rmin2 = Rmin / zoom;
101 double Rmax2 = Rmax / zoom;
102 double Imin2 = Imin / zoom;
103 double Imax2 = Imax / zoom;
104 double Rshift = Tr - (Rmin2 + Rmax2) / 2;
105 double Ishift = Ti - (Imin2 + Imax2) / 2;
111 // affichage d'un rectangle autour de la nouvelle zone d'intérêt
112 double x_factor = (w.width - 1) / (Rmax - Rmin);
113 double y_factor = (w.height - 1) / (Imax - Imin);
115 w.drawRect((Rmin2 - Rmin) * x_factor, (Imax - Imin2) * y_factor,
116 (Rmax2 - Rmin) * x_factor, (Imax - Imax2) * y_factor);
125 int main(int argc, char *argv[])
127 QApplication app(argc, argv);
128 DrawingWindow win(mandel, 800, 800);