1 #include <DrawingWindow.h>
2 #include <QApplication>
6 // nombre max d'itérations
8 // zone d'intérêt par défaut
18 const parameters initial_parameters = {
28 static inline double sqr(double x)
33 static int check_point(parameters& p, double cr, double ci)
37 if (sqr(cr + 1) + zi2 < 1.0 / 16.0)
39 double x4 = cr - 1.0 / 4.0;
40 double q = sqr(x4) + zi2;
41 if (q * (q + x4) < zi2 / 4.0)
47 for (i = 0 ; i < p.maxiter && zr2 + zi2 < 4 ; i++) {
48 zi = 2 * zr * zi + ci;
56 static void set_color(DrawingWindow& w, parameters& p, int i)
58 double rouge, vert, bleu;
60 rouge = vert = bleu = 0.0;
62 int ii = (p.maxiter - 1 - i) % 96;
70 rouge = (ii - 32) / 32.0;
75 vert = (ii - 64) / 32.0;
80 w.setColor(rouge, vert, bleu);
83 // Fonction de dessin de l'ensemble de Madelbrot, dans la zone
84 // spécifiée, et avec la précision souhgaitée.
85 static void do_mandel(DrawingWindow& w, parameters& p)
87 int x, y; // le pixel considéré
88 double cr, ci; // le complexe correspondant
90 for (y = 0 ; y < w.height ; y++) {
91 ci = p.Imax - y * p.Iscale;
94 int i0 = check_point(p, cr, ci);
95 for (x = 1 ; x < w.width ; x++) {
96 cr = p.Rmin + x * p.Rscale;
97 int i = check_point(p, cr, ci);
100 w.drawLine(x0, y, x - 1, y);
106 w.drawLine(x0, y, w.width - 1, y);
110 // Fonction de dessin principale, calcule la zone d'intérêt, appelle
111 // do_mandel(), pour dessiner l'ensemle, et permet le zoom.
112 static void mandel(DrawingWindow &w)
114 parameters p = initial_parameters;
116 p.Rscale = (p.Rmax - p.Rmin) / (w.width - 1);
117 p.Iscale = (p.Imax - p.Imin) / (w.height - 1);
121 w.drawText(5, 5, "Cliquer sur l'image pour zoomer");
125 w.waitMousePress(x, y, button);
127 // calcul des coordonnées du point cliqué
128 double Tr = p.Rmin + x * p.Rscale;
129 double Ti = p.Imax - y * p.Rscale;
131 // calcul de la nouvelle zone d'intérêt :
132 // zoom ×2 en direction du point cliqué
134 double Rmin2 = p.Rmin / zoom;
135 double Rmax2 = p.Rmax / zoom;
136 double Imin2 = p.Imin / zoom;
137 double Imax2 = p.Imax / zoom;
138 double Rshift = Tr - (Rmin2 + Rmax2) / 2;
139 double Ishift = Ti - (Imin2 + Imax2) / 2;
145 // affichage d'un rectangle autour de la nouvelle zone d'intérêt
147 w.drawRect((Rmin2 - p.Rmin) / p.Rscale, (p.Imax - Imin2) / p.Iscale,
148 (Rmax2 - p.Rmin) / p.Rscale, (p.Imax - Imax2) / p.Iscale);
157 int main(int argc, char *argv[])
159 QApplication app(argc, argv);
160 DrawingWindow win(mandel, 800, 800);