-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvizualizer.cpp
More file actions
112 lines (95 loc) · 2.75 KB
/
vizualizer.cpp
File metadata and controls
112 lines (95 loc) · 2.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#include "vizualizer.h"
Vizualizer::Vizualizer(QObject * parent)
: QObject(parent)
{
size = 8;
max_size = 64;
min_size = 8;
width = 1280;
height = 1080;
max_width = 1000;
max_height = 1000;
current_pos_x = 0;
current_pos_y = 0;
cell_types.assign(max_width, vector<Type>(max_height, UNDEFINED));
init_types();
}
void Vizualizer::onKeyPressed(long long code)
{
if (code == 43 || code == 61)
increase_size();
else if (code == 45)
decrease_size();
else if (code == 16777234) {
current_pos_x = max(0, current_pos_x - width / size / 3);
} else if (code == 16777235) {
current_pos_y = max(0, current_pos_y - height / size / 3);
} else if (code == 16777236) {
current_pos_x = min(max_width - width / size, current_pos_x + width / size / 3);
} else if (code == 16777237) {
current_pos_y = min(max_height - height / size, current_pos_y + height / size / 3);
}
}
QImage Vizualizer::onPaint()
{
QImage q(width, height, QImage::Format_RGB32);
q.fill(Qt::white);
for (int i = 0; i < width / size; ++i) {
for (int j = 0; j < height / size; ++j) {
int x = current_pos_x + i;
int y = current_pos_y + j;
if (x < 0 || y < 0 || x >= max_width || y >= max_height)
continue;
drawCell(i * size, j * size, x, y, q);
}
}
return q;
}
void Vizualizer::drawCell(int x, int y, int cell_x, int cell_y, QImage & q) {
const vector<vector<QRgb> > & rgbs = cell(cell_x, cell_y);
int cnt = size / min_size;
for (int k = 0; k < size; ++k) {
for (int l = 0; l < size; ++l) {
q.setPixel(x + k, y + l, rgbs[k / cnt][l / cnt]);
}
}
}
vector<vector<QRgb>> const & Vizualizer::cell(int x, int y) {
return cells[cell_types[x][y]];
}
void Vizualizer::init_types() {
add_ally();
add_lava();
add_undefined();
add_none();
}
void Vizualizer::add_ally() {
vector<vector<QRgb> > rgbs(size, vector<QRgb> (size, GREEN));
rgbs[2][2] = rgbs[1][3] = rgbs[5][2] = rgbs[6][3] = BLACK;
rgbs[3][6] = rgbs[4][6] = BLACK;
cells.push_back(rgbs);
}
void Vizualizer::add_lava() {
vector<vector<QRgb> > rgbs(size, vector<QRgb> (size, RED));
cells.push_back(rgbs);
}
void Vizualizer::add_none() {
vector<vector<QRgb> > rgbs(size, vector<QRgb> (size, WHITE));
cells.push_back(rgbs);
}
void Vizualizer::add_undefined() {
vector<vector<QRgb> > rgbs(size, vector<QRgb> (size, BLACK));
cells.push_back(rgbs);
}
void Vizualizer::increase_size() {
if (size >= max_size)
size = max_size;
else
size <<= 1;
}
void Vizualizer::decrease_size() {
if (size <= min_size)
size = min_size;
else
size >>= 1;
}