-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmap.cpp
85 lines (71 loc) · 1.75 KB
/
map.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
#include "map.h"
#include <QtCore>
Map::Map(QObject *parent) :
QObject(parent)
{
this->width = 0;
this->height = 0;
this->resolution = 0.0;
this->_cells = NULL;
}
Map::Map(qint32 width, qint32 height, qreal resolution, QVector<quint8> *cells, QObject *parent) : QObject(parent)
{
this->width = width;
this->height = height;
this->resolution = resolution;
this->_cells = cells;
}
Map::~Map()
{
delete this->_cells;
}
bool Map::isValid()
{
return this->width > 0 && this->height > 0 && this->resolution > 0.0 && !this->_cells->isEmpty() && this->width*this->height == this->_cells->size();
}
quint8 Map::at(qint32 x, qint32 y)
{
if (x >= 0 && x < this->width && y >= 0 && y < this->height) {
qint32 idx = y*this->width+x;
if (idx < this->_cells->size()) {
return this->_cells->at(idx);
}
}
return 0;
}
QPoint Map::coordinateOf(QPointF point)
{
qint32 x = round(point.x()/this->resolution);
qint32 y = round(point.y()/this->resolution);
if (x < 0) { x = 0; }
else if (x >= this->width) { x = this->width-1; }
if (y < 0) { y = 0; }
else if (y >= this->height) {y = this->height-1; }
return QPoint(x, y);
}
QPointF Map::positionOf(QPoint coordinate)
{
return QPointF(coordinate.x()*this->resolution, coordinate.y()*this->resolution);
}
QVector<quint8> *Map::cells()
{
return this->_cells;
}
void Map::setCells(QVector<quint8> *cells)
{
if (this->_cells) {
delete this->_cells;
}
this->_cells = cells;
}
void Map::appendCells(QVector<quint8> *cells)
{
if (!this->_cells) {
this->_cells = cells;
}
else {
for (int i = 0; i < cells->size(); i++) {
this->_cells->append(cells->at(i));
}
}
}