-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNode.cpp
102 lines (83 loc) · 2.21 KB
/
Node.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#include "Node.h"
Node::Node(int id) {
this->id = id;
this->latitude = 0;
this->longitude = 0;
this->path = nullptr;
this->visited = false;
this->distance = numeric_limits<double>::infinity();
}
Node::Node(int id, double latitude, double longitude) {
this->id = id;
this->latitude = latitude;
this->longitude = longitude;
this->path = nullptr;
this->visited = false;
this->distance = numeric_limits<double>::infinity();
}
int Node::getId() const {
return id;
}
list<Edge*> Node::getEdges() const {
return edges;
}
void Node::addEdge(Edge* edge) {
edges.push_back(edge);
}
bool Node::isVisited() const {
return visited;
}
void Node::setVisited(bool v) {
this->visited = v;
}
double Node::getLatitude() const {
return latitude;
}
double Node::getLongitude() const {
return longitude;
}
double Node::getDistanceTo(Node* other) const {
int id2 = other->getId();
for(Edge* edge : edges){
if(edge->getNode1() == id2 || edge->getNode2() == id2){
return edge->getDistance();
}
}
return this->getHaversineDistanceTo(other);
}
double Node::getHaversineDistanceTo(Node* node) const {
double rad_lat1 = this->latitude * M_PI / 180;
double rad_lon1 = this->longitude * M_PI / 180;
double rad_lat2 = node->getLatitude() * M_PI / 180;
double rad_lon2 = node->getLongitude() * M_PI / 180;
double delta_lat = rad_lat2 - rad_lat1;
double delta_lon = rad_lon2 - rad_lon1;
double aux = pow(sin(delta_lat/2), 2) + cos(rad_lat1) * cos(rad_lat2) * pow(sin(delta_lon/2), 2);
double c = 2.0 * atan2(sqrt(aux), sqrt(1.0-aux));
double earthradius = 6371000;
return earthradius * c;
}
void Node::setPath(Edge* p){
this->path = p;
}
Edge* Node::getPath() const {
return path;
}
void Node::setDistance(double d){
this->distance = d;
}
double Node::getDistance() const {
return distance;
}
bool Node::operator<(Node& node) const {
return this->distance < node.distance;
}
Edge* Node::getEdgeTo(Node* node) const {
int id2 = node->getId();
for(Edge* edge : edges){
if(edge->getNode1() == id2 || edge->getNode2() == id2){
return edge;
}
}
return nullptr;
}