-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmainwindow.cpp
287 lines (267 loc) · 8.35 KB
/
mainwindow.cpp
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <iostream>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
void MainWindow::paintEvent(QPaintEvent *) {
if(p.empty()) {return;}
QPainter painter(this);
QPen pen(QColor(128,128,128));
painter.setPen(pen);
const QPoint start(20, 80);
const QPoint dir[] = {QPoint(0,W), QPoint(W,0)};
int length = 0;
int steps = 0;
QPoint pos = start;
QRect rect(pos, QSize(W, W));
for(int i = 0; i < p.size(); i++) {
for(int j = 0; j < p[i].size(); j++) {
if (p[i][j].g > degree) { //TODO
pos = start + i * dir[0] + j * dir[1];
painter.setBrush(QColor(255, 255, 255));
rect.moveTo(pos);
painter.drawRect(rect);
continue;
}
if(p[i][j].val == "O") {
pos = start + i*dir[0] + j*dir[1];
painter.setBrush(QColor(0,0,0));
rect.moveTo(pos);
painter.drawRect(rect);
} else if (p[i][j].val == "X"){
pos = start + i*dir[0] + j*dir[1];
painter.setBrush(QColor(255,255,255));
rect.moveTo(pos);
painter.drawRect(rect);
} else if (p[i][j].val == "S"){
pos = start + i*dir[0] + j*dir[1];
painter.setBrush(QColor(0,0,255));
rect.moveTo(pos);
painter.drawRect(rect);
} else if (p[i][j].val == "E") {
pos = start + i*dir[0] + j*dir[1];
painter.setBrush(QColor(255,0,0));
rect.moveTo(pos);
painter.drawRect(rect);
} else if (p[i][j].val == "A"){
pos = start + i*dir[0] + j*dir[1];
painter.setBrush(QColor(0,255,0));
rect.moveTo(pos);
painter.drawRect(rect);
length++;
} else {
pos = start + i*dir[0] + j*dir[1];
painter.setBrush(QColor(255,255,0));
rect.moveTo(pos);
painter.drawRect(rect);
steps++;
}
}
}
std::string str = length == 0 ? "unreachable" : std::to_string(length);
ui->distance->setText(QString::fromStdString(str));
ui->steps->setText(QString::fromStdString(std::to_string(steps + length)));
}
void MainWindow::mousePressEvent(QMouseEvent *event) {
if(event->button() == Qt::LeftButton && event->pos().x() >= 20 && event->pos().x() < 20 + W * N
&& event->pos().y() >= 80 && event->pos().y() < 80 + W * M) {
QPoint point = event->pos();
int x = (point.x()-20)/W;
int y = (point.y()-80)/W;
if(select_start) {
std::cout << (point.x()-20)/W << "," << (point.y()-80)/W << std::endl;
static int xt,yt;
static std::string tt;
if (x1!=-1 && p[xt][yt].val == "S") {
p[xt][yt].val = tt;
}
y1 = x;
x1 = y;
xt = x1;
yt = y1;
tt = p[x1][y1].val;
p[x1][y1].val = "S";
select_start = false;
QString s = QString::fromStdString("(" + std::to_string(x) + "," + std::to_string(y) + ")");
ui->start_point->setText(s);
} else if(select_end) {
std::cout << (point.x()-20)/W << "," << (point.y()-80)/W << std::endl;
static int xt,yt;
static std::string tt;
if (x2!=-1 && p[xt][yt].val == "E") {
p[xt][yt].val = tt;
}
x2 = y;
y2 = x;
xt = x2;
yt = y2;
tt = p[x2][y2].val;
p[x2][y2].val = "E";
select_end = false;
QString s = QString::fromStdString("(" + std::to_string(x) + "," + std::to_string(y) + ")");
ui->end_point->setText(s);
}
update();
}
}
void MainWindow::mouseMoveEvent(QMouseEvent *event) {
if(event->buttons() & Qt::RightButton && event->pos().x() >= 20 && event->pos().x() < 20 + W * N
&& event->pos().y() >= 80 && event->pos().y() < 80 + W * M) {
QPoint point = event->pos();
int x = (point.x()-20)/W;
int y = (point.y()-80)/W;
std::cout << "p[y][x]:" << y << "," << x << std::endl;
p[y][x].val = "X";
update();
}
if(event->buttons() & Qt::LeftButton && event->pos().x() >= 20 && event->pos().x() < 20 + W * N
&& event->pos().y() >= 80 && event->pos().y() < 80 + W * M) {
QPoint point = event->pos();
int x = (point.x()-20)/W;
int y = (point.y()-80)/W;
std::cout << "p[y][x]:" << y << "," << x << std::endl;
p[y][x].val = "O";
update();
}
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::resizeWindow() {
W = ui->size_edit->text().toInt();
M = ui->row_edit->text().toInt();
N = ui->col_edit->text().toInt();
int width = this->size().width();
int height = this->size().height();
if(height < W*M + 100) {
height = W*M + 100;
} else if(W*M + 100 < 600) {
height = 600;
}
if (width < W*N + 40) {
width = W*N + 40;
} else if(W*N + 40 < 800) {
width = 800;
}
this->resize(width, height);
// blank map
p.clear();
for (int i = 0; i < M; i++) {
std::vector<algorithm::Node> pt;
for (int j = 0; j < N; j++) {
algorithm::Node node(i, j);
node.val = "X";
pt.push_back(node);
}
p.push_back(pt);
}
update();
}
void MainWindow::createRandomMap() {
if (isPaint == false) {
return;
}
int row = ui->row_edit->text().toInt();
int col = ui->col_edit->text().toInt();
double sq = ui->density_edit->text().toDouble();
std::default_random_engine generator;
std::uniform_real_distribution<double> distribution(0, sq);
typedef std::chrono::high_resolution_clock myclock;
myclock::time_point beginning = myclock::now();
myclock::duration d = myclock::now() - beginning;
unsigned seed = d.count();
generator.seed(seed);
p.clear();
for (int i = 0; i < row; i++) {
std::vector<algorithm::Node> pt;
for (int j = 0; j < col; j++) {
int k = distribution(generator) < 1;
algorithm::Node node(i, j);
if(k) {
node.val = "O";
} else {
node.val = "X";
}
pt.push_back(node);
}
p.push_back(pt);
}
update();
}
void MainWindow::on_paint_btn_clicked()
{
isPaint = true;
resizeWindow();
}
void MainWindow::on_random_btn_clicked()
{
createRandomMap();
}
void MainWindow::on_set_start_btn_clicked()
{
select_start = true;
}
void MainWindow::on_set_end_btn_clicked()
{
select_end = true;
}
void MainWindow::on_start_btn_clicked()
{
if(p.empty() || (x1==x2&&y1==y2) || x1<0 || x2<0 || y1<0 ||y2<0 ||x1>=M||x2>=M||y1>=N||y2>=N) {
return;
}
QString str = ui->algo_cbx->currentText();
int (algorithm::distance::*algo)(int, int, int, int) = NULL;
if(str == "BFS") {
algo = &algorithm::distance::bfs;
} else if (str == "Manhattan") {
algo = &algorithm::distance::manhattan;
} else if (str == "Euclidean") {
algo = &algorithm::distance::euclidean;
} else if (str == "Chebyshev") {
algo = &algorithm::distance::chebyshev;
}
algorithm a(p);
a.setAlgo(algo);
p = a.start();
timer = new QTimer();
reset = false;
connect(timer, SIGNAL(timeout()), this, SLOT(updateMap()));
timer->setInterval(ui->interval_edit->text().toInt());
timer->start();
}
void MainWindow::updateMap() {
static int i = 0;
if (reset) {
i = 0;
timer->stop();
reset = false;
}
if (i <= p[x2][y2].g) {
degree = i;
update();
i++;
} else {
timer->stop();
i = 0;
reset = false;
}
}
void MainWindow::on_clear_btn_clicked()
{
reset = true;
if (p.empty()) return;
for (int i = 0; i < p.size(); i++) {
for (int j = 0; j < p[0].size(); j++) {
if (p[i][j].val != "E" && p[i][j].val != "S" && p[i][j].val != "O" && p[i][j].val != "X") {
p[i][j].val = "X";
}
}
}
update();
}