-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdistance.cpp
More file actions
38 lines (32 loc) · 922 Bytes
/
Copy pathdistance.cpp
File metadata and controls
38 lines (32 loc) · 922 Bytes
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
// distance.cpp
#include <cmath>
#include <iostream>
const float radius = 3959.9;
float DegreeToRadians(float n)
{
float s;
s = n* (M_PI/180);
return s;
}
float DistanceOfTwoLocations(float lat1, float lon1, float lat2, float lon2)
{
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;
}