Logo AND Algorithmique Numérique Distribuée

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