2015-12-08 04:51:55 +08:00
|
|
|
#pragma once
|
2015-06-19 16:04:42 +08:00
|
|
|
|
|
|
|
#include <cmath>
|
2017-02-18 18:54:37 +08:00
|
|
|
#include <limits>
|
2015-06-19 16:04:42 +08:00
|
|
|
|
|
|
|
#include "point.hpp"
|
|
|
|
|
|
|
|
namespace kd {
|
|
|
|
namespace math {
|
|
|
|
|
|
|
|
using byte = unsigned char;
|
|
|
|
|
|
|
|
// returns the squared distance between two points
|
2017-02-18 18:54:37 +08:00
|
|
|
template <class T>
|
|
|
|
T distance_sq(const Point<T>& a, const Point<T>& b) {
|
|
|
|
auto dx = a.longitude - b.longitude;
|
|
|
|
auto dy = a.latitude - b.latitude;
|
|
|
|
return dx * dx + dy * dy;
|
2015-06-19 16:04:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// returns the distance between two points
|
2017-02-18 18:54:37 +08:00
|
|
|
template <class T>
|
|
|
|
T distance(const Point<T>& a, const Point<T>& b) {
|
|
|
|
return std::sqrt(distance_sq(a, b));
|
2015-06-19 16:04:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// returns the distance between two points looking at a specific axis
|
|
|
|
// \param axis 0 if abscissa else 1 if ordinate
|
|
|
|
template <class T>
|
2017-02-18 18:54:37 +08:00
|
|
|
T axial_distance(const Point<T>& a, const Point<T>& b, byte axis) {
|
|
|
|
return axis == 0 ? a.longitude - b.longitude : a.latitude - b.latitude;
|
2015-06-19 16:04:42 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|