-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgrid_map.cpp
More file actions
146 lines (124 loc) · 2.74 KB
/
grid_map.cpp
File metadata and controls
146 lines (124 loc) · 2.74 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
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
#include "grid_map.h"
namespace NAVIGATION
{
Node::Node(const int& r, const int& c, const int& type)
:row(r), col(c), node_type(type)
{
}
void Node::drawNode() const
{
int left = ORIX + col * LEN;
int right = left + LEN;
int top = ORIY + row * LEN;
int bottom = top + LEN;
fillrectangle(left, top, right, bottom);
}
bool GridMapBase::GridMapInit(const Node& start, const Node& end)
{
if (outOfMap(start) || outOfMap(end))
{
cout << "invalid statr or end" << endl;
return false;
}
// build map
for (int i = 0; i < ROWS; i++)
{
vector<Node> temp_row;
for (int j = 0; j < COLS; j++)
{
Node temp_node(i, j);
temp_row.emplace_back(temp_node);
}
grid_map.emplace_back(temp_row);
}
// update start & end
grid_map[start.row][start.col].node_type = NodeType::start;
grid_map[end.row][end.col].node_type = NodeType::end;
start_node = &grid_map[start.row][start.col];
end_node = &grid_map[end.row][end.col];
return true;
}
void GridMapBase::printMap()
{
cout << "Rows: " << ROWS << ", ColS: " << COLS << endl;
cout << "start: [" << start_node->row << "," << start_node->col << "]\n" <<
"end: [" << end_node->row << "," << end_node->col << "]" << endl;
for (int i = 0; i < ROWS; i++)
{
for (int j = 0; j < COLS; j++)
{
if (grid_map[i][j].node_type == NodeType::normal)
{
cout << " ";
}
else
{
cout << grid_map[i][j].node_type << " ";
}
}
cout << endl;
}
cout << endl;
}
void GridMapBase::drawMap()
{
setbkcolor(WHITE);
setlinecolor(BLACK);
setlinestyle(PS_SOLID, 1);
cleardevice();
BeginBatchDraw();
// normal
setfillcolor(WHITE);
for (int i = 0; i < ROWS; i++) {
for (int j = 0; j < COLS; j++)
{
grid_map[i][j].drawNode();
}
}
// start
setfillcolor(RED);
start_node->drawNode();
// end
setfillcolor(BLUE);
end_node->drawNode();
// obs
setfillcolor(BROWN);
for (const auto& obs_node : obs_nodes) {
obs_node->drawNode();
}
EndBatchDraw();
}
bool GridMapBase::outOfMap(const Node& node)
{
return (node.col < 0 || node.col >= COLS || node.row < 0 || node.row >= ROWS);
}
GridMap::GridMap(const Node& start, const Node& end)
{
assert(GridMapInit(start, end));
addObs();
printMap();
drawMap();
}
void GridMap::addObs()
{
Node* temp_node;
for (int i = 10; i < 35; i++)
{
temp_node = &grid_map[10][i];
obs_nodes.insert(temp_node);
temp_node->node_type = NodeType::obs;
}
for (int i = 10; i < 35; i++)
{
temp_node = &grid_map[34][i];
obs_nodes.insert(temp_node);
temp_node->node_type = NodeType::obs;
}
for (int i = 10; i < 35; i++)
{
temp_node = &grid_map[i][35];
obs_nodes.insert(temp_node);
temp_node->node_type = NodeType::obs;
}
}
}