1 #include <DrawingWindow.h>
2 #include <QApplication>
9 // nombre max d'itérations
11 // zone d'intérêt par défaut
21 const parameters initial_parameters = {
31 static inline double sqr(double x)
36 static int check_point(parameters& p, double cr, double ci)
40 if (sqr(cr + 1) + zi2 < 1.0 / 16.0)
42 double x4 = cr - 1.0 / 4.0;
43 double q = sqr(x4) + zi2;
44 if (q * (q + x4) < zi2 / 4.0)
50 for (i = 0 ; i < p.maxiter && zr2 + zi2 < 4 ; i++) {
51 zi = 2 * zr * zi + ci;
59 // int check_point(parameters& p, int x, int y)
61 // double cr = p.Rmin + x * p.Rscale;
62 // double ci = p.Imax - y * p.Iscale;
63 // return check_point(p, cr, ci);
66 static void set_color(DrawingWindow& w, parameters& p, int i)
68 double rouge, vert, bleu;
70 rouge = vert = bleu = 0.0;
72 int ii = (p.maxiter - 1 - i) % 96;
80 rouge = (ii - 32) / 32.0;
85 vert = (ii - 64) / 32.0;
90 w.setColor(rouge, vert, bleu);
93 // Fonction de dessin de l'ensemble de Madelbrot, dans la zone
94 // spécifiée, et avec la précision souhgaitée.
95 static void do_mandel(DrawingWindow& w, parameters& p)
97 int x, y; // le pixel considéré
98 double cr, ci; // le complexe correspondant
100 for (y = 0 ; y < w.height ; y++) {
101 ci = p.Imax - y * p.Iscale;
104 int i0 = check_point(p, cr, ci);
105 for (x = 1 ; x < w.width ; x++) {
106 cr = p.Rmin + x * p.Rscale;
107 int i = check_point(p, cr, ci);
110 w.drawLine(x0, y, x - 1, y);
116 w.drawLine(x0, y, w.width - 1, y);
120 // Fonction de dessin principale, calcule la zone d'intérêt, appelle
121 // do_mandel(), pour dessiner l'ensemle, et permet le zoom.
122 static void mandel(DrawingWindow &w)
124 parameters p = initial_parameters;
126 p.Rscale = (p.Rmax - p.Rmin) / (w.width - 1);
127 p.Iscale = (p.Imax - p.Imin) / (w.height - 1);
131 w.drawText(5, 5, "Cliquer sur l'image pour zoomer");
135 w.waitMousePress(x, y, button);
137 // calcul des coordonnées du point cliqué
138 double Tr = p.Rmin + x * p.Rscale;
139 double Ti = p.Imax - y * p.Rscale;
141 // calcul de la nouvelle zone d'intérêt :
142 // zoom ×2 en direction du point cliqué
144 double Rmin2 = p.Rmin / zoom;
145 double Rmax2 = p.Rmax / zoom;
146 double Imin2 = p.Imin / zoom;
147 double Imax2 = p.Imax / zoom;
148 double Rshift = Tr - (Rmin2 + Rmax2) / 2;
149 double Ishift = Ti - (Imin2 + Imax2) / 2;
155 // affichage d'un rectangle autour de la nouvelle zone d'intérêt
157 w.drawRect((Rmin2 - p.Rmin) / p.Rscale, (p.Imax - Imin2) / p.Iscale,
158 (Rmax2 - p.Rmin) / p.Rscale, (p.Imax - Imax2) / p.Iscale);
167 int main(int argc, char *argv[])
169 QApplication app(argc, argv);
170 DrawingWindow win(mandel, 800, 800);