-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvector-operations.cpp
More file actions
59 lines (40 loc) · 1.31 KB
/
Copy pathvector-operations.cpp
File metadata and controls
59 lines (40 loc) · 1.31 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
#include "vector-operations.h"
float euclidianDistance(float x1, float y1, float x2, float y2){
float dx = x2-x1;
float dy = y2-y1;
return sqrt(dx*dx + dy*dy);
}
std::vector<int> minEuclidianCoord(const std::vector<std::vector<int>> &queue, const std::vector<int> &targetCoords){
//returns coordinate closest to the target
std::vector<float> euclidianVector;
for(auto coord: queue){
euclidianVector.push_back(euclidianDistance(coord[0],coord[1],targetCoords[0],targetCoords[1]));
}
auto it = std::min_element(euclidianVector.begin(),euclidianVector.end());
size_t minIndex = it-euclidianVector.begin();
return queue[minIndex];
}
std::vector<std::vector<int>> getAdjacentCoords(int x, int y, int width, int height){
std::vector<std::vector<int>> adjacentCoords;
if(x-1 >= 0){
adjacentCoords.push_back({x-1,y});
}
if(x+1 < width){
adjacentCoords.push_back({x+1,y});
}
if(y-1 >=0){
adjacentCoords.push_back({x,y-1});
}
if(y+1 < height){
adjacentCoords.push_back({x,y+1});
}
return adjacentCoords;
}
bool coordIn(const std::vector<int> &coord, const std::vector<std::vector<int>> &seenVector){
for(auto x: seenVector){
if(coord==x){
return true;
}
}
return false;
}