Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
.
[graphlib.git] / exemple.cpp
1 /*
2  * Pour compiler et exécuter
3  * =========================
4  *
5  * 1. Créer le fichier exemple.pro :
6  *      +------------------------------------------------------------+
7  *      |TEMPLATE = app                                              |
8  *      |TARGET = hello                                              |
9  *      |CONFIG += qt                                                |
10  *      |CONFIG += debug                                             |
11  *      |HEADERS += DrawingWindow.h                                  |
12  *      |SOURCES += DrawingWindow.cpp                                |
13  *      |SOURCES += exemple.cpp                                      |
14  *      +------------------------------------------------------------+
15  *
16  * 2. Créer le fichier Makefile avec la commande :
17  *      $ qmake-qt4 exemple.pro
18  *    ou tout simplement :
19  *      $ qmake-qt4
20  *
21  * 3. Compiler avec la commande :
22  *      $ make exemple
23  *    ou tou simplement :
24  *      $ make
25  *
26  * 4. Exécuter le programme avec la commande :
27  *      $ ./exemple
28  */
29
30 #include <QApplication>
31 #include <DrawingWindow.h>
32 #include <algorithm>
33 #include <cstdlib>
34
35 float frand()
36 {
37     return rand() / (float )RAND_MAX;
38 }
39
40 void exemple1(DrawingWindow &w)
41 {
42     const int cx = w.width / 2;
43     const int cy = w.height / 2;
44     const int delta = 5;
45     for (int x = 0; x < w.width; x += delta) {
46         w.drawLine(cx, cy, x, 0);
47         w.drawLine(cx, cy, w.width - 1 - x, w.height - 1);
48     }
49     for (int y = 0; y < w.height; y += delta) {
50         w.drawLine(cx, cy, 0, w.height - 1 - y);
51         w.drawLine(cx, cy, w.width - 1, y);
52     }
53 }
54
55 void exemple2(DrawingWindow &w)
56 {
57     int width = std::min(w.width, w.height) / 2;
58     for (int z = 0; z <= width; z++) {
59         float r, g, b;
60         float s = 3.0 * z / width;
61         if (z <= width / 3.0) {
62             r = 1.0 - s;
63             g = s;
64             b = 0.0;
65         } else if (z <= 2.0 * width / 3.0) {
66             s -= 1.0;
67             r = 0.0;
68             g = 1.0 - s;
69             b = s;
70         } else {
71             s -= 2.0;
72             r = s;
73             g = 0.0;
74             b = 1.0 - s;
75         }
76         w.setColor(r, g, b);
77         w.drawRect(z, z, w.width - 1 - z, w.height - 1 - z);
78     }
79 }
80
81 void exemple3(DrawingWindow &w)
82 {
83     while (1) {
84         int x1 = rand() % w.width;
85         int y1 = rand() % w.height;
86         int x2 = rand() % w.width;
87         int y2 = rand() % w.height;
88         w.setColor(frand(), frand(), frand());
89         w.drawLine(x1, y1, x2, y2);
90         w.sync();
91     }
92 }
93
94 int main(int argc, char *argv[])
95 {
96     QApplication application(argc, argv);
97     DrawingWindow window1(exemple1, 640, 480);
98     DrawingWindow window2(exemple2, 640, 480);
99     DrawingWindow window3(exemple3, 640, 480);
100
101     window1.setWindowTitle("Exemple 1");
102     window2.setWindowTitle("Exemple 2");
103     window3.setWindowTitle("Exemple 3");
104
105     window1.show();
106     window2.show();
107     window3.show();
108
109     return application.exec();
110 }