Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
.
[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         }
46         if ((y += 10) >= w.height) {
47             y = 0;
48             c = !c;
49             if (!--count) break;
50 //             std::cerr << "loooooooooooooooooooooop "
51 //                       << y << " (" << c << ")\n";
52         }
53     }
54 }
55
56 void mandel(DrawingWindow &w)
57 {
58     /* paramètres par défaut */
59     int larg = w.width;
60     int haut = w.height;
61     float Rmin = -2.05;
62     float Rmax = 0.55;
63     float Imin = -1.3;
64     float Imax = 1.3;
65
66     int maxiter = 100;
67
68     int x, y;                   /* le pixel considéré */
69     float cr, ci;               /* le complexe correspondant */
70     float zr, zi;               /* pour calculer la suite */
71     float zr2, zi2;
72     float pr, pi;               /* taille d'un pixel */
73     float rouge, vert, bleu;
74     int i;
75
76     pr = (Rmax - Rmin) / larg;
77     pi = (Imax - Imin) / haut;
78
79     cr = Rmin;
80     for (x = 0; x < larg; x++) {
81         ci = Imin;
82         for (y = 0; y < haut; y++) {
83             /* z_1 = c */
84             zr = cr;
85             zi = ci;
86             for (i = 1; i <= maxiter; i++) {
87                 zr2 = zr * zr;
88                 zi2 = zi * zi;
89                 if (zr2 + zi2 >= 4) {
90                     /* |z| >= 2 : on sort de la boucle */
91                     break;
92                 }
93                 /* on calcule le z suivant */
94                 zi = 2*zr*zi + ci;
95                 zr = zr2 - zi2 + cr;
96             }
97                 /* on est sorti trop tôt du for(...):
98                    on affiche le pixel d'un couleur en fonction 
99                    de i */
100                  if (i <= maxiter / 2) {
101                     /* entre rouge et vert */
102                     vert = (2.0 * i) / maxiter;
103                     rouge = 1.0 - vert;
104                     bleu = 0.0;
105                 } else if (i <= maxiter) {
106                     /* entre vert et bleu */
107                     rouge = 0.0;
108                     bleu = (2.0 * i) / maxiter - 1.0;
109                     vert = 1.0 - bleu;
110                 } else /* (i > maxiter) */
111                     rouge = vert = bleu = 0.0;
112                  w.setColor(rouge, vert, bleu);
113                  w.drawPoint(x, y);
114
115             ci += pi;
116         }
117         cr += pr;
118     }
119 }
120
121 void lines(DrawingWindow &w)
122 {
123     int n = 100000;
124     int xmax = w.width;
125     int ymax = w.height;
126     while (n-- > 0) {
127         double r = rand() / (float )RAND_MAX;
128         double g = rand() / (float )RAND_MAX;
129         double b = rand() / (float )RAND_MAX;
130         int x1 = rand() % xmax;
131         int y1 = rand() % ymax;
132         int x2 = rand() % xmax;
133         int y2 = rand() % ymax;
134         w.setColor(r, g, b);
135         w.drawLine(x1, y1, x2, y2);
136         w.sync();
137     }
138 }
139
140 void rectangles(DrawingWindow &w)
141 {
142     int d = 5;
143     int z = (w.width > w.height ? w.height : w.width) / 2;
144     z = d * (z / d);
145     while (z > 0) {
146         w.drawRect(z, z, w.width - 1 - z, w.height - 1 - z);
147         z -= d;
148     }
149 }
150
151 int main(int argc, char *argv[])
152 {
153     const int w = 1000;
154     const int h = 700;
155     QApplication application(argc, argv);
156
157     DrawingWindow dl(lines, w, h);
158     dl.show();
159
160     DrawingWindow dr(rectangles, w, h);
161     dr.show();
162
163     const int nf = 1;
164     const int nm = 1;
165     DrawingWindow *dw[nf + nm];
166
167     for (int i = 0; i < nf; ++i)
168         dw[i] = new DrawingWindow(flip, w, h);
169     for (int i = nf; i < nf + nm; ++i)
170         dw[i] = new DrawingWindow(mandel, w, h);
171
172     for (int i = 0; i < nf + nm; ++i)
173         dw[i]->show();
174
175     return application.exec();
176 }