-
Notifications
You must be signed in to change notification settings - Fork 0
/
astartest_2d_2.cc
123 lines (97 loc) · 3.34 KB
/
astartest_2d_2.cc
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
// This function shows the usage of AStar_Custom_dist(..) function to specify
// using 'taxicab' distance instead of polar distance.
#include <iomanip>
#include <iostream>
#include <string>
#include <vector>
#include <cmath>
#include <cstdlib>
#include <ctime>
#define BRDSIZ 40
bool SEEDED = false;
std::string thepath[BRDSIZ + 1][BRDSIZ + 1];
#include "Utilities.h"
int myrand(void) { // Random number generator: 0-BRDSIZ range.
if(!SEEDED) {
unsigned int iseed = (unsigned int)::time(NULL);
::srand(iseed);
SEEDED = true;
}
return ::rand() % BRDSIZ;
}
double My_Distance(const std::vector<int> &a, const std::vector<int> &b) {
double res = 0.0;
/*
//Polar distance.
for(unsigned int i=0; i<a.size(); ++i) res += static_cast<double>((b[i] - a[i])*(b[i] - a[i]));
res = sqrt(res);
return res;
*/
// Taxicab distance.
for(unsigned int i = 0; i < a.size(); ++i) res += static_cast<double>(fabs(b[i] - a[i]));
return res;
}
int main() {
std::vector<std::vector<int>> final_path;
std::vector<int> beginning;
beginning.push_back(myrand()); // x
beginning.push_back(myrand()); // y
// beginning.push_back(0); //x
// beginning.push_back(52); //y
std::vector<int> end;
end.push_back(myrand()); // x
end.push_back(myrand()); // y
// end.push_back(45); //x
// end.push_back(62); //y
while(beginning == end) {
end.clear();
end.push_back(myrand()); // x
end.push_back(myrand()); // y
}
// Colour the board unused.
for(unsigned int i = 0; i <= (BRDSIZ); ++i)
for(unsigned int j = 0; j <= (BRDSIZ); ++j) {
thepath[i][j] = "░";
}
final_path = AStar_Custom_dist(beginning, end, &My_Distance);
std::cout << "Start node: " << beginning[0] << " " << beginning[1] << std::endl;
std::cout << "Target node: " << end[0] << " " << end[1] << std::endl;
// Plot the path with filled circles.
for(unsigned int i = 0; i < final_path.size(); ++i) {
size_t x = final_path[i][0];
size_t y = final_path[i][1];
thepath[x][y] = "◉";
}
// Fill the endpoints with special glyphs.
thepath[end[0]][end[1]] = "◬";
thepath[beginning[0]][beginning[1]] = "◦";
// Draw the board.
for(int j = (BRDSIZ); j >= 0; --j) {
std::cout << std::endl;
std::cout.width(2);
std::cout << (j);
std::cout.width(1);
std::cout << " ";
for(unsigned int i = 0; i <= BRDSIZ; ++i) {
std::cout << thepath[i][j] << " ";
}
}
std::cout << std::endl << std::endl << " ";
for(unsigned int i = 0; i <= BRDSIZ; ++i) {
if(static_cast<int>(i / 10) == 0) {
std::cout << " ";
} else {
std::cout << static_cast<int>(i / 10) << " ";
}
}
std::cout << std::endl << " ";
for(unsigned int i = 0; i <= BRDSIZ; ++i) std::cout << i % 10 << " ";
std::cout << std::endl;
std::cout << "Key:" << std::endl;
std::cout << " ░ -- Unvisited node (it was visitable.)" << std::endl;
std::cout << " █ -- Blocked node (it was not visitable.)" << std::endl;
std::cout << " ◉ -- Trajectory node." << std::endl;
std::cout << " ◬ -- Target node." << std::endl;
std::cout << " ◦ -- Start node." << std::endl;
return 0;
}