-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdistance.cpp
More file actions
57 lines (46 loc) · 1.29 KB
/
Copy pathdistance.cpp
File metadata and controls
57 lines (46 loc) · 1.29 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
// distance.cpp
#include <cmath>
#include <iostream>
#include <string>
const float radius = 3959.9;
float SignAdjust(std::string bearing, float value)
{
if ((bearing == "/S" && value !=0) || (bearing =="/E" && value!=0))
{
value *=-1;
return value;
}
else
{
return value;
}
}
float DegreeToRadians(float n)
{
float s;
s = n* (M_PI/180);
return s;
}
float DistanceOfTwoLocations(float lat1, float lon1, float lat2, float lon2) {
if (lat1==0 && lat2==0 && lon1==180 and lon2==-180) {return 0.0;}
if (lat1==0 && lat2==0 && lon1==-180 and lon2==180) {return 0.0;}
float a, c, d;
float phi1, phi2, dphi, dlam;
// phi1 = lat1 * (M_PI/180);
phi1 = DegreeToRadians(lat1);
// std::cout << phi1 << std::endl;
// phi2 = lat2 * (M_PI/180);
phi2 = DegreeToRadians(lat2);
// std::cout << phi2 << std::endl;
dphi = phi2 - phi1;
// std::cout << dphi << std::endl;
// dlam = (lon2-lon1) * (M_PI/180);
dlam = DegreeToRadians(lon2-lon1);
// std::cout << dlam << std::endl;
a = std::sin(dphi/2) * std::sin(dphi/2) +
std::cos(phi1) * std::cos(phi2) *
std::sin(dlam/2) * std::sin(dlam/2);
c = 2 * std::atan2(std::sqrt(a),std::sqrt(1-a));
d = radius * c;
return d;
}