-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdeg.h
38 lines (33 loc) · 1.11 KB
/
deg.h
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
#ifndef DEG_H
#define DEG_H
#include <math.h>
#include <cmath>
#define earthRadiusKm 6371.0
// This function converts decimal degrees to radians
double deg2rad(double deg) {
return (deg * M_PI / 180);
}
// This function converts radians to decimal degrees
double rad2deg(double rad) {
return (rad * 180 / M_PI);
}
/**
* Returns the distance between two points on the Earth.
* Direct translation from http://en.wikipedia.org/wiki/Haversine_formula
* @param lat1d Latitude of the first point in degrees
* @param lon1d Longitude of the first point in degrees
* @param lat2d Latitude of the second point in degrees
* @param lon2d Longitude of the second point in degrees
* @return The distance between the two points in meters
*/
double distanceEarth(double lat1d, double lon1d, double lat2d, double lon2d) {
double lat1r, lon1r, lat2r, lon2r, u, v;
lat1r = deg2rad(lat1d);
lon1r = deg2rad(lon1d);
lat2r = deg2rad(lat2d);
lon2r = deg2rad(lon2d);
u = sin((lat2r - lat1r)/2);
v = sin((lon2r - lon1r)/2);
return 2.0 * earthRadiusKm * asin(sqrt(u * u + cos(lat1r) * cos(lat2r) * v * v)) * 1000.;
}
#endif