-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathastar.cpp
More file actions
167 lines (157 loc) · 7.48 KB
/
astar.cpp
File metadata and controls
167 lines (157 loc) · 7.48 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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#include <SFML/Main.hpp>
#include <SFML/Graphics.hpp>
#include "gridgraph.hpp"
#include <queue>
#include <unistd.h>
#include <iostream>
#include <tgmath.h>
#define WIDTH 400
#define HEIGHT 400
static const sf::Vector2i start(0, 0);
void astar(GridGraph& graph, sf::Vector2i start, sf::Vector2i goal)
{
auto node_cmp = [] (std::pair<GridGraph::GridPoint*, sf::Vector2i> a, std::pair<GridGraph::GridPoint*, sf::Vector2i> b) {
return a.first->weight > b.first->weight;
};
std::priority_queue<
std::pair<GridGraph::GridPoint*, sf::Vector2i>,
std::vector<std::pair<GridGraph::GridPoint*, sf::Vector2i>>,
decltype(node_cmp)> fringe(node_cmp)
;
auto d_heur = [&goal] (int x, int y) { return std::sqrt(std::pow(x - goal.x, 2) + std::pow(y - goal.y, 2)); };
auto find_fringe = [&graph, &fringe, d_heur] (sf::Vector2i pos) {
auto cur = graph.get_node(pos.x, pos.y);
if (pos.x < graph.size.x - 1) {
auto node = graph.get_node(pos.x + 1, pos.y);
if (node->state == GridGraph::GridState::FREE || node->state == GridGraph::GridState::GOAL) {
if (node->state == GridGraph::GridState::GOAL) {
node->parent = pos;
fringe.push(std::pair<GridGraph::GridPoint*, sf::Vector2i>(node, sf::Vector2i(pos.x, pos.y - 1)));
return;
}
node->state = GridGraph::GridState::VISITED;
node->parent = pos;
fringe.push(std::pair<GridGraph::GridPoint*, sf::Vector2i>(node, sf::Vector2i(pos.x + 1, pos.y)));
}
}
if (pos.x > 0) {
auto node = graph.get_node(pos.x - 1, pos.y);
if (node->state == GridGraph::GridState::FREE || node->state == GridGraph::GridState::GOAL) {
if (node->state == GridGraph::GridState::GOAL) {
node->parent = pos;
fringe.push(std::pair<GridGraph::GridPoint*, sf::Vector2i>(node, sf::Vector2i(pos.x, pos.y - 1)));
return;
}
node->state = GridGraph::GridState::VISITED;
node->parent = pos;
fringe.push(std::pair<GridGraph::GridPoint*, sf::Vector2i>(node, sf::Vector2i(pos.x - 1, pos.y)));
}
}
if (pos.y < graph.size.y - 1) {
auto node = graph.get_node(pos.x, pos.y + 1);
if (node->state == GridGraph::GridState::FREE || node->state == GridGraph::GridState::GOAL) {
if (node->state == GridGraph::GridState::GOAL) {
node->parent = pos;
fringe.push(std::pair<GridGraph::GridPoint*, sf::Vector2i>(node, sf::Vector2i(pos.x, pos.y - 1)));
return;
}
node->state = GridGraph::GridState::VISITED;
node->parent = pos;
fringe.push(std::pair<GridGraph::GridPoint*, sf::Vector2i>(node, sf::Vector2i(pos.x, pos.y + 1)));
}
}
if (pos.y > 0) {
auto node = graph.get_node(pos.x, pos.y - 1);
if (node->state == GridGraph::GridState::FREE || node->state == GridGraph::GridState::GOAL) {
if (node->state == GridGraph::GridState::GOAL) {
node->parent = pos;
fringe.push(std::pair<GridGraph::GridPoint*, sf::Vector2i>(node, sf::Vector2i(pos.x, pos.y - 1)));
return;
}
node->state = GridGraph::GridState::VISITED;
node->parent = pos;
fringe.push(std::pair<GridGraph::GridPoint*, sf::Vector2i>(node, sf::Vector2i(pos.x, pos.y - 1)));
}
}
};
for (size_t i = 0; i < graph.size.y; i++) {
for (size_t j = 0; j < graph.size.x; j++) {
graph.grid[i][j].weight = d_heur(j, i);
}
}
graph.set_node(start.x, start.y, GridGraph::GridState::VISITED, d_heur(0, 0));
graph.set_node(goal.x, goal.y, GridGraph::GridState::GOAL, 0);
auto cur = std::pair<GridGraph::GridPoint*, sf::Vector2i>(graph.get_node(start.x, start.y), sf::Vector2i(start.x, start.y));
sf::RenderWindow window(sf::VideoMode(WIDTH, HEIGHT), "Graph Window");
window.setFramerateLimit(60);
while (window.isOpen()) {
sf::Event event;
while (window.pollEvent(event)) {
if (event.type == sf::Event::Closed)
window.close();
}
window.setActive();
window.draw(graph);
window.display();
usleep(100000);
find_fringe(cur.second);
cur = fringe.top();
fringe.pop();
if (cur.first->state == GridGraph::GridState::GOAL) {
break;
}
}
graph.get_node(0, 0)->state = GridGraph::GridState::PATH;
while (cur.first->parent != start) {
cur.first = graph.get_node(cur.first->parent.x, cur.first->parent.y);
cur.first->state = GridGraph::GridState::PATH;
}
window.draw(graph);
window.display();
usleep(10000000);
}
int main(int argc, char* argv[])
{
// sf::RenderWindow window(sf::VideoMode(WIDTH, HEIGHT), "Graph Window");
// window.setFramerateLimit(60);
GridGraph graph(20, 20);
graph.set_node(10, 10, GridGraph::GridState::BLOCKED, 0);
graph.set_node(10, 11, GridGraph::GridState::BLOCKED, 0);
graph.set_node(11, 11, GridGraph::GridState::BLOCKED, 0);
graph.set_node(12, 11, GridGraph::GridState::BLOCKED, 0);
graph.set_node(12, 12, GridGraph::GridState::BLOCKED, 0);
graph.set_node(13, 13, GridGraph::GridState::BLOCKED, 0);
graph.set_node(14, 14, GridGraph::GridState::BLOCKED, 0);
graph.set_node(15, 15, GridGraph::GridState::BLOCKED, 0);
graph.set_node(16, 16, GridGraph::GridState::BLOCKED, 0);
graph.set_node(17, 17, GridGraph::GridState::BLOCKED, 0);
graph.set_node(18, 18, GridGraph::GridState::BLOCKED, 0);
graph.set_node(5, 10, GridGraph::GridState::BLOCKED, 0);
graph.set_node(6, 11, GridGraph::GridState::BLOCKED, 0);
graph.set_node(7, 11, GridGraph::GridState::BLOCKED, 0);
graph.set_node(8, 11, GridGraph::GridState::BLOCKED, 0);
graph.set_node(9, 12, GridGraph::GridState::BLOCKED, 0);
graph.set_node(10, 13, GridGraph::GridState::BLOCKED, 0);
graph.set_node(11, 14, GridGraph::GridState::BLOCKED, 0);
graph.set_node(12, 15, GridGraph::GridState::BLOCKED, 0);
graph.set_node(13, 16, GridGraph::GridState::BLOCKED, 0);
graph.set_node(14, 17, GridGraph::GridState::BLOCKED, 0);
graph.set_node(0, 3, GridGraph::GridState::BLOCKED, 0);
graph.set_node(1, 3, GridGraph::GridState::BLOCKED, 0);
graph.set_node(2, 3, GridGraph::GridState::BLOCKED, 0);
graph.set_node(3, 3, GridGraph::GridState::BLOCKED, 0);
graph.set_node(4, 3, GridGraph::GridState::BLOCKED, 0);
graph.set_node(5, 3, GridGraph::GridState::BLOCKED, 0);
graph.set_node(6, 3, GridGraph::GridState::BLOCKED, 0);
graph.set_node(7, 3, GridGraph::GridState::BLOCKED, 0);
graph.set_node(8, 3, GridGraph::GridState::BLOCKED, 0);
graph.set_node(8, 4, GridGraph::GridState::BLOCKED, 0);
graph.set_node(8, 5, GridGraph::GridState::BLOCKED, 0);
graph.set_node(9, 4, GridGraph::GridState::BLOCKED, 0);
graph.set_node(9, 5, GridGraph::GridState::BLOCKED, 0);
graph.set_node(10, 5, GridGraph::GridState::BLOCKED, 0);
graph.set_node(3, 15, GridGraph::GridState::BLOCKED, 0);
graph.set_node(4, 15, GridGraph::GridState::BLOCKED, 0);
graph.set_node(2, 15, GridGraph::GridState::BLOCKED, 0);
astar(graph, sf::Vector2i(0, 0), sf::Vector2i(3, 17));
}