Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
bc1a62eaf8229b36e5c8ac29c7001b82fec6027e
[graphlib.git] / test / hello.cpp
1 /*
2  * Pour compiler
3  * =============
4  *
5  * 1. Créer le fichier hello.pro :
6  *      +------------------------------------------------------------+
7  *      |TARGET = hello                                              |
8  *      |CONFIG += qt debug                                          |
9  *      |SOURCES += hello.cc                                         |
10  *      +------------------------------------------------------------+
11  *
12  * 2. Créer le fichier Makefile avec la commande :
13  *      $ qmake -makefile hello.pro
14  *    ou tout simplement :
15  *      $ qmake -makefile
16  *
17  * 3. Compiler avec la commande :
18  *      $ make hello
19  *    ou tou simplement :
20  *      $ make
21  */
22
23
24 #include <QApplication>
25 #include <DrawingWindow.h>
26
27 #include <iostream>
28
29 void flip(DrawingWindow &w)
30 {
31     std::cout << "[ " << w.width << " x " << w.height << " ]\n";
32
33     int c = 0;
34     int y = 0;
35 //     int h = w.height;
36 //     int w = w.width;
37     int count = 50;//1 << 31;
38     while (1) {
39 //         std::cerr << "loooooooooooooooooooooop "
40 //                   << y << " (" << c << ")\n";
41         w.setColor(c, c, c);
42         for (int yy = y; yy < y + 10; yy++) {
43             for (int x = 0; x < w.width; x++)
44                 w.drawPoint(x, yy);
45             w.sync();
46         }
47         if ((y += 10) >= w.height) {
48             y = 0;
49             c = !c;
50             if (!--count) break;
51 //             std::cerr << "loooooooooooooooooooooop "
52 //                       << y << " (" << c << ")\n";
53         }
54     }
55 }
56
57 void mandel(DrawingWindow &w)
58 {
59     /* paramètres par défaut */
60     int larg = w.width;
61     int haut = w.height;
62     float Rmin = -2.05;
63     float Rmax = 0.55;
64     float Imin = -1.3;
65     float Imax = 1.3;
66
67     int maxiter = 100;
68
69     int x, y;                   /* le pixel considéré */
70     float cr, ci;               /* le complexe correspondant */
71     float zr, zi;               /* pour calculer la suite */
72     float zr2, zi2;
73     float pr, pi;               /* taille d'un pixel */
74     float rouge, vert, bleu;
75     int i;
76
77     pr = (Rmax - Rmin) / larg;
78     pi = (Imax - Imin) / haut;
79
80     cr = Rmin;
81     for (x = 0; x < larg; x++) {
82         ci = Imin;
83         for (y = 0; y < haut; y++) {
84             /* z_1 = c */
85             zr = cr;
86             zi = ci;
87             for (i = 1; i <= maxiter; i++) {
88                 zr2 = zr * zr;
89                 zi2 = zi * zi;
90                 if (zr2 + zi2 >= 4) {
91                     /* |z| >= 2 : on sort de la boucle */
92                     break;
93                 }
94                 /* on calcule le z suivant */
95                 zi = 2*zr*zi + ci;
96                 zr = zr2 - zi2 + cr;
97             }
98                 /* on est sorti trop tôt du for(...):
99                    on affiche le pixel d'un couleur en fonction 
100                    de i */
101                  if (i <= maxiter / 2) {
102                     /* entre rouge et vert */
103                     vert = (2.0 * i) / maxiter;
104                     rouge = 1.0 - vert;
105                     bleu = 0.0;
106                 } else if (i <= maxiter) {
107                     /* entre vert et bleu */
108                     rouge = 0.0;
109                     bleu = (2.0 * i) / maxiter - 1.0;
110                     vert = 1.0 - bleu;
111                 } else /* (i > maxiter) */
112                     rouge = vert = bleu = 0.0;
113                  w.setColor(rouge, vert, bleu);
114                  w.drawPoint(x, y);
115
116             ci += pi;
117         }
118         cr += pr;
119 //         w.sync();
120     }
121 }
122
123 void lines(DrawingWindow &w)
124 {
125     int n = 100000;
126     int xmax = w.width;
127     int ymax = w.height;
128     while (n-- > 0) {
129         double r = rand() / (float )RAND_MAX;
130         double g = rand() / (float )RAND_MAX;
131         double b = rand() / (float )RAND_MAX;
132         int x1 = rand() % xmax;
133         int y1 = rand() % ymax;
134         int x2 = rand() % xmax;
135         int y2 = rand() % ymax;
136         w.setColor(r, g, b);
137         w.drawLine(x1, y1, x2, y2);
138         w.sync();
139     }
140 }
141
142 void rectangles(DrawingWindow &w)
143 {
144     int d = 5;
145     int z = (w.width > w.height ? w.height : w.width) / 2;
146     z = d * (z / d);
147     while (z > 0) {
148         w.drawRect(z, z, w.width - 1 - z, w.height - 1 - z);
149         z -= d;
150     }
151 }
152
153 int main(int argc, char *argv[])
154 {
155     const int w = 1000;
156     const int h = 700;
157     QApplication application(argc, argv);
158
159     const int nf = 1;
160     const int nm = 1;
161     DrawingWindow *dw[nf + nm];
162
163     for (int i = 0; i < nf; ++i)
164         dw[i] = new DrawingWindow(flip, w, h);
165     for (int i = nf; i < nf + nm; ++i)
166         dw[i] = new DrawingWindow(mandel, w, h);
167
168     for (int i = 0; i < nf + nm; ++i)
169         dw[i]->show();
170
171     DrawingWindow dr(rectangles, w, h);
172     dr.show();
173
174     DrawingWindow dl(lines, w, h);
175     dl.show();
176
177     return application.exec();
178 }