3 static class Parameters {
4 // nombre max d'itérations
6 // zone d'intérêt par défaut
16 static final Parameters initial_parameters;
18 initial_parameters = new Parameters();
19 initial_parameters.maxiter = 1000;
20 initial_parameters.Rmin = -2.05;
21 initial_parameters.Rmax = 0.55;
22 initial_parameters.Imin = -1.3;
23 initial_parameters.Imax = 1.3;
24 initial_parameters.Rscale = 0.0;
25 initial_parameters.Iscale = 0.0;
28 static 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 float rouge, vert, bleu;
60 rouge = vert = bleu = 0.0f;
62 int ii = (p.maxiter - 1 - i) % 96;
70 rouge = (ii - 32) / 32.0f;
75 vert = (ii - 64) / 32.0f;
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
89 for (y = 0 ; y < w.height ; y++) {
90 ci = p.Imax - y * p.Iscale;
93 int i0 = check_point(p, cr, ci);
94 for (x = 1 ; x < w.width ; x++) {
95 cr = p.Rmin + x * p.Rscale;
96 int i = check_point(p, cr, ci);
99 w.drawLine(x0, y, x - 1, y);
105 w.drawLine(x0, y, w.width - 1, y);
109 // Fonction de dessin principale, calcule la zone d'intérêt, appelle
110 // do_mandel(), pour dessiner l'ensemble, et permet le zoom.
111 static void mandel(DrawingWindow w)
113 Parameters p = initial_parameters;
115 p.Rscale = (p.Rmax - p.Rmin) / (w.width - 1);
116 p.Iscale = (p.Imax - p.Imin) / (w.height - 1);
119 w.drawText(5, 15, "Cliquer sur l'image pour zoomer");
121 int x = w.getMouseX();
122 int y = w.getMouseY();
123 // calcul des coordonnées du point cliqué
124 double Tr = p.Rmin + x * p.Rscale;
125 double Ti = p.Imax - y * p.Rscale;
126 // calcul de la nouvelle zone d'intérêt :
127 // zoom ×2 en direction du point cliqué
129 double Rmin2 = p.Rmin / zoom;
130 double Rmax2 = p.Rmax / zoom;
131 double Imin2 = p.Imin / zoom;
132 double Imax2 = p.Imax / zoom;
133 double Rshift = Tr - (Rmin2 + Rmax2) / 2;
134 double Ishift = Ti - (Imin2 + Imax2) / 2;
139 // affichage d'un rectangle autour de la nouvelle zone d'intérêt
141 w.drawRect((int)Math.round((Rmin2 - p.Rmin) / p.Rscale),
142 (int)Math.round((p.Imax - Imin2) / p.Iscale),
143 (int)Math.round((Rmax2 - p.Rmin) / p.Rscale),
144 (int)Math.round((p.Imax - Imax2) / p.Iscale));
152 public static void main(String[] args) {
153 DrawingWindow w = new DrawingWindow("Mandelbrot' set", 800, 800);