Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
.
[graphlib.git] / exemple.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 #include <algorithm>
27 #include <cstdlib>
28
29 float frand()
30 {
31     return rand() / (float )RAND_MAX;
32 }
33
34 void exemple1(DrawingWindow &w)
35 {
36     const int cx = w.width / 2;
37     const int cy = w.height / 2;
38     const int delta = 5;
39     for (int x = 0; x < w.width; x += delta) {
40         w.drawLine(cx, cy, x, 0);
41         w.drawLine(cx, cy, w.width - 1 - x, w.height - 1);
42     }
43     for (int y = 0; y < w.height; y += delta) {
44         w.drawLine(cx, cy, 0, w.height - 1 - y);
45         w.drawLine(cx, cy, w.width - 1, y);
46     }
47 }
48
49 void exemple2(DrawingWindow &w)
50 {
51     int width = std::min(w.width, w.height) / 2;
52     for (int z = 0; z <= width; z++) {
53         float r, g, b;
54         float s = 3.0 * z / width;
55         if (z <= width / 3.0) {
56             r = 1.0 - s;
57             g = s;
58             b = 0.0;
59         } else if (z <= 2.0 * width / 3.0) {
60             s -= 1.0;
61             r = 0.0;
62             g = 1.0 - s;
63             b = s;
64         } else {
65             s -= 2.0;
66             r = s;
67             g = 0.0;
68             b = 1.0 - s;
69         }
70         w.setColor(r, g, b);
71         w.drawRect(z, z, w.width - 1 - z, w.height - 1 - z);
72     }
73 }
74
75 void exemple3(DrawingWindow &w)
76 {
77     while (1) {
78         int x1 = rand() % w.width;
79         int y1 = rand() % w.height;
80         int x2 = rand() % w.width;
81         int y2 = rand() % w.height;
82         w.setColor(frand(), frand(), frand());
83         w.drawLine(x1, y1, x2, y2);
84         w.sync();
85     }
86 }
87
88 int main(int argc, char *argv[])
89 {
90     QApplication application(argc, argv);
91     DrawingWindow window1(exemple1, 640, 480);
92     DrawingWindow window2(exemple2, 640, 480);
93     DrawingWindow window3(exemple3, 640, 480);
94
95     window1.setWindowTitle("Exemple 1");
96     window2.setWindowTitle("Exemple 2");
97     window3.setWindowTitle("Exemple 3");
98
99     window1.show();
100     window2.show();
101     window3.show();
102
103     return application.exec();
104 }