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