memgraph/data_structures/kdtree/math.hpp

44 lines
887 B
C++
Raw Normal View History

2015-06-19 16:04:42 +08:00
#ifndef GEOAPI_KDTREE_MATH_HPP
#define GEOAPI_KDTREE_MATH_HPP
#include <limits>
#include <cmath>
#include "point.hpp"
namespace kd {
namespace math {
using byte = unsigned char;
// returns the squared distance between two points
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;
}
// returns the distance between two points
template<class T>
T distance(const Point<T>& a, const Point<T>& b)
{
return std::sqrt(distance_sq(a, b));
}
// returns the distance between two points looking at a specific axis
// \param axis 0 if abscissa else 1 if ordinate
template <class T>
T axial_distance(const Point<T>& a, const Point<T>& b, byte axis)
{
return axis == 0 ?
a.longitude - b.longitude:
a.latitude - b.latitude;
}
}
}
#endif