DovesLapTimer 4.0.0
GPS-based lap timing Arduino library — go-karts to race cars
Loading...
Searching...
No Matches
GeoMath.h
Go to the documentation of this file.
1
11#ifndef _GEOMATH_H
12#define _GEOMATH_H
13
14#include <math.h>
15
16// M_PI is not part of the C/C++ standard — it's a POSIX/BSD extension that
17// glibc happens to expose by default but stricter libcs (e.g. under
18// -std=c++NN / __STRICT_ANSI__) hide. Define a fallback so the library
19// compiles everywhere.
20#ifndef M_PI
21#define M_PI 3.14159265358979323846
22#endif
23
24static const double GEOMATH_RADIUS_EARTH = 6371.0 * 1000; // meters
25
26static inline double geoHaversine(double lat1, double lon1, double lat2, double lon2) {
27 double lat1Rad = lat1 * M_PI / 180.0;
28 double lon1Rad = lon1 * M_PI / 180.0;
29 double lat2Rad = lat2 * M_PI / 180.0;
30 double lon2Rad = lon2 * M_PI / 180.0;
31
32 double sinHalfDLat = sin((lat2Rad - lat1Rad) / 2);
33 double sinHalfDLon = sin((lon2Rad - lon1Rad) / 2);
34
35 double a = sinHalfDLat * sinHalfDLat + cos(lat1Rad) * cos(lat2Rad) * sinHalfDLon * sinHalfDLon;
36 double c = 2 * atan2(sqrt(a), sqrt(1 - a));
37
38 return GEOMATH_RADIUS_EARTH * c;
39}
40
41static inline double geoHaversine3D(double prevLat, double prevLng, double prevAlt, double currentLat, double currentLng, double currentAlt) {
42 double dist = geoHaversine(prevLat, prevLng, currentLat, currentLng);
43 double altDiff = currentAlt - prevAlt;
44 return sqrt(dist * dist + altDiff * altDiff);
45}
46
47#endif
static double geoHaversine(double lat1, double lon1, double lat2, double lon2)
Definition GeoMath.h:26
static double geoHaversine3D(double prevLat, double prevLng, double prevAlt, double currentLat, double currentLng, double currentAlt)
Definition GeoMath.h:41
static const double GEOMATH_RADIUS_EARTH
Definition GeoMath.h:24
#define M_PI
Definition GeoMath.h:21