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
16static const double GEOMATH_RADIUS_EARTH = 6371.0 * 1000; // meters
17
18static inline double geoHaversine(double lat1, double lon1, double lat2, double lon2) {
19 double lat1Rad = lat1 * M_PI / 180.0;
20 double lon1Rad = lon1 * M_PI / 180.0;
21 double lat2Rad = lat2 * M_PI / 180.0;
22 double lon2Rad = lon2 * M_PI / 180.0;
23
24 double sinHalfDLat = sin((lat2Rad - lat1Rad) / 2);
25 double sinHalfDLon = sin((lon2Rad - lon1Rad) / 2);
26
27 double a = sinHalfDLat * sinHalfDLat + cos(lat1Rad) * cos(lat2Rad) * sinHalfDLon * sinHalfDLon;
28 double c = 2 * atan2(sqrt(a), sqrt(1 - a));
29
30 return GEOMATH_RADIUS_EARTH * c;
31}
32
33static inline double geoHaversine3D(double prevLat, double prevLng, double prevAlt, double currentLat, double currentLng, double currentAlt) {
34 double dist = geoHaversine(prevLat, prevLng, currentLat, currentLng);
35 double altDiff = currentAlt - prevAlt;
36 return sqrt(dist * dist + altDiff * altDiff);
37}
38
39#endif
static double geoHaversine(double lat1, double lon1, double lat2, double lon2)
Definition GeoMath.h:18
static double geoHaversine3D(double prevLat, double prevLng, double prevAlt, double currentLat, double currentLng, double currentAlt)
Definition GeoMath.h:33
static const double GEOMATH_RADIUS_EARTH
Definition GeoMath.h:16