Loading...
Searching...
No Matches
distances.h
1//
2// Created by henri on 03/11/22.
3//
4
5#ifndef IDOL_DISTANCES_H
6#define IDOL_DISTANCES_H
7
8#include <cmath>
9#include <numbers>
10
11namespace idol {
12
16 double euclidean(const std::pair<double, double> &a, const std::pair<double, double> &b) {
17 return std::sqrt(std::pow(a.first - b.first, 2) + std::pow(a.second - b.second, 2));
18 }
19
24 double haversine(const std::pair<double, double> &t_a, const std::pair<double, double> &t_b) {
25 auto [lat1, lon1] = t_a;
26 auto [lat2, lon2] = t_b;
27 // distance between latitudes
28 // and longitudes
29 const double dLat = (lat2 - lat1) * M_PI / 180.0;
30 const double dLon = (lon2 - lon1) * M_PI / 180.0;
31
32 // convert to radians
33 lat1 = (lat1) * M_PI / 180.0;
34 lat2 = (lat2) * M_PI / 180.0;
35
36 // apply formulae
37 const double a =
38 std::pow(std::sin(dLat / 2), 2) + std::pow(std::sin(dLon / 2), 2) * std::cos(lat1) * std::cos(lat2);
39 // double rad = 6371;
40 const double rad = 3958.755866;
41 const double c = 2 * std::asin(std::sqrt(a));
42
43 return rad * c;
44 }
45
46}
47
48#endif //IDOL_DISTANCES_H