-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgps.c
34 lines (29 loc) · 1.13 KB
/
gps.c
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
#include <math.h>
#include "gps.h"
#define earthRadiusKm 6371.0
/* =============================== GPS Location ============================= */
// Copied from https://stackoverflow.com/questions/10198985/calculating-the-distance-between-2-latitudes-and-longitudes-that-are-saved-in-a
double deg2rad(double deg)
{
return (deg * M_PI / 180);
}
/**
* Returns the distance between two points on the Earth.
* Direct translation from http://en.wikipedia.org/wiki/Haversine_formula
* @param lat1 Latitude of the first point in degrees
* @param lon1 Longitude of the first point in degrees
* @param lat2 Latitude of the second point in degrees
* @param lon2 Longitude of the second point in degrees
* @return The distance between the two points in kilometers
*/
double distanceOnEarth(double lat1, double lon1, double lat2, double lon2)
{
double lat1r, lon1r, lat2r, lon2r, u, v;
lat1r = deg2rad(lat1);
lon1r = deg2rad(lon1);
lat2r = deg2rad(lat2);
lon2r = deg2rad(lon2);
u = sin((lat2r - lat1r) / 2);
v = sin((lon2r - lon1r) / 2);
return 2.0 * earthRadiusKm * asin(sqrt(u * u + cos(lat1r) * cos(lat2r) * v * v));
}