Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
cf598aa55567ae660a1d330c1beefa6b48ee5cbe
[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 int 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     return 0;
54 }
55
56 int 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     return 0;
120 }
121
122 int lines(DrawingWindow &w)
123 {
124     int n = 100000;
125     int xmax = w.width();
126     int ymax = w.height();
127     while (n-- > 0) {
128         double r = rand() / (float )RAND_MAX;
129         double g = rand() / (float )RAND_MAX;
130         double b = rand() / (float )RAND_MAX;
131         int x1 = rand() % xmax;
132         int y1 = rand() % ymax;
133         int x2 = rand() % xmax;
134         int y2 = rand() % ymax;
135         w.setColor(r, g, b);
136         w.drawLine(x1, y1, x2, y2);
137     }
138     return 0;
139 }
140
141 int main(int argc, char *argv[])
142 {
143     const int w = 1000;
144     const int h = 700;
145     QApplication application(argc, argv);
146
147     DrawingWindow dd(lines, w, h);
148     dd.show();
149     return application.exec();
150
151     const int nf = 1;
152     const int nm = 1;
153     DrawingWindow *dw[nf + nm];
154
155     for (int i = 0; i < nf; ++i)
156         dw[i] = new DrawingWindow(flip, w, h);
157     for (int i = nf; i < nf + nm; ++i)
158         dw[i] = new DrawingWindow(mandel, w, h);
159
160     for (int i = 0; i < nf + nm; ++i)
161         dw[i]->show();
162
163     return application.exec();
164 }