Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
.
[graphlib.git] / chateaux / chateaux.cpp
1 #include <QApplication>
2 #include <DrawingWindow.h>
3 #include <cmath>
4 #include <iostream>
5 #include <ctime>
6 #include <cstdlib>
7
8 /* Note : les coordonnées réelles vont de -100 à +100 en abscisse, et
9  *  de -10 à +140 en ordonnée
10  */
11
12 const float rXMin = -100.0;
13 const float rXMax = 100.0;
14 const float rYMin = -10.0;
15 const float rYMax = 140.0;
16
17 const float hauteurMin = 10;
18 const float hauteurMax = 130;
19 const float largeurMin = 40;
20 const float largeurMax = 150;
21
22 const float positionChateau1 = -85.0;
23 const float positionChateau2 = 85.0;
24
25 /* Retourne un nombre pseudo-aléatoire compris entre 0 et le paramètre
26  * 'max' (exclus)
27  */
28 float frand(float min, float max)
29 {
30     static bool first = true;
31     if (first) {
32         srand(time(NULL));
33         first = false;
34     }
35     return min + (max - min)* (rand() / (RAND_MAX + 1.0));
36 }
37
38 // conversion coordonnées réelles -> coordonnées fenêtre
39 int rtowX(const DrawingWindow& w, float rx)
40 {
41     return (int )roundf(w.width * (rx - rXMin) / (rXMax - rXMin + 1.0));
42 }
43
44 int rtowY(const DrawingWindow& w, float ry)
45 {
46     return (int )roundf(w.height * (rYMax - ry) / (rYMax - rYMin + 1.0));
47 }
48
49 // conversion coordonnées réelles -> coordonnées fenêtre
50 float wtorX(const DrawingWindow& w, int wx)
51 {
52     return (rXMax - rXMin + 1.0) * wx / w.width + rXMin;
53 }
54
55 float wtorY(const DrawingWindow& w, int wy)
56 {
57     return -(rYMax - rYMin + 1.0) * wy / w.height - rYMax;
58 }
59
60 void dessineTerrain(DrawingWindow& w, float largeur, float hauteur)
61 {
62     float l = largeur / 2.0;
63     float h = hauteur;
64     int y0 = rtowY(w, 0) + 1;
65     int xmin = rtowX(w, -l) - 1;
66     int xmax = rtowX(w, l) + 1;
67     w.setColor("forestgreen");
68     for (int x = xmin; x <= xmax; x++) {
69         float rx = wtorX(w, x) / l;
70         float ry = h * (1.0 - rx * rx);
71         int y = rtowY(w, ry);
72         if (y <= y0)
73             w.drawLine(x, y0, x, y);
74     }
75     w.setColor("maroon");
76     w.fillRect(0, y0 + 1, w.width - 1, w.height - 1);
77 }
78
79 void dessineChateau(DrawingWindow& w, float position)
80 {
81     w.setColor("black");
82     w.setColor("darkslategray");
83     int y1 = rtowY(w, 0);
84     int h0 = rtowY(w, 3);
85     int h1 = rtowY(w, 4);
86     for (int i = 0; i < 7; i++) {
87         int h = i % 2 ? h0 : h1;
88         int x1 = rtowX(w, position + i - 3.5);
89         int x2 = rtowX(w, position + i - 2.5) - 1;
90         w.fillRect(x1, y1, x2, h);
91     }
92     w.setColor("dimgray");
93     h0 = rtowY(w, 6);
94     h1 = rtowY(w, 7);
95     for (int i = 0; i < 5; i++) {
96         int h = i % 2 ? h0 : h1;
97         int x1 = rtowX(w, position + i - 8.5);
98         int x2 = rtowX(w, position + i - 7.5) - 1;        
99         w.fillRect(x1, y1, x2, h);
100         x1 = rtowX(w, position + i + 3.5);
101         x2 = rtowX(w, position + i + 4.5) - 1;        
102         w.fillRect(x1, y1, x2, h);
103     }
104 }
105
106 void dessineExplosion(DrawingWindow& w, float rx, float ry)
107 {
108     const int maxray = rtowX(w, 5) - rtowX(w, 0);
109     // 1/2 rouge -> rouge -> jaune
110     const int x = rtowX(w, rx);
111     const int y = rtowY(w, ry);
112     int i;
113     for (i = 0; i <= maxray / 3; i++) {
114         w.setColor(0.5 + 3.0 * i / (2.0 * maxray), 0.0, 0.0);
115         w.drawCircle(x, y, i);
116         w.msleep(20);
117     }
118     for (/* i */; i < maxray; i++) {
119         w.setColor(1.0, 1.5 * i / maxray - 0.5, 0.0);
120         w.drawCircle(x, y, i);
121         w.msleep(20);
122     }
123     w.setColor("white");
124     for (i = 0; i < maxray; i++) {
125         w.drawCircle(x, y, i);
126         w.msleep(20);
127     }
128     w.fillCircle(x, y, maxray - 1);
129 }
130
131 void jeu(DrawingWindow& w)
132 {
133     dessineTerrain(w, frand(largeurMin, largeurMax),
134                    frand(hauteurMin, hauteurMax));
135     dessineChateau(w, positionChateau1);
136     dessineChateau(w, positionChateau2);
137
138     while (1)
139         dessineExplosion(w, frand(rXMin, rXMax), frand(rYMin, rYMax));
140     
141 }
142
143 int main(int argc, char *argv[])
144 {
145     QApplication application(argc, argv);
146     DrawingWindow window(jeu, 640, 480);
147     window.show();
148     return application.exec();
149 }
150