point: add octile distance function

This commit is contained in:
Danny Robson 2015-09-22 18:32:11 +10:00
parent 56b5c34b5d
commit c7637fd627
2 changed files with 27 additions and 0 deletions

View File

@ -50,6 +50,9 @@ namespace util {
template <size_t S, typename T, typename U>
constexpr typename std::common_type<T,U>::type distance2 (point<S,T>, point<S,U>);
template <size_t S, typename T, typename U>
constexpr typename std::common_type<T,U>::type octile (point<S,T>, point<S,U>);
template <size_t S, typename T, typename U>
constexpr typename std::common_type<T,U>::type manhattan (point<S,T>, point<S,U>);

View File

@ -73,6 +73,30 @@ namespace util {
}
//-------------------------------------------------------------------------
template <size_t S, typename T, typename U>
constexpr typename std::common_type<T,U>::type
octile (point<S,T> a, point<S,U> b)
{
using type_t = typename std::common_type<T,U>::type;
static_assert (!std::is_integral<type_t>::value,
"octile requires more than integer precision");
const type_t D1 = 1;
const type_t D2 = std::sqrt (type_t {2});
auto diff = util::abs (a - b);
// distance for axis-aligned walks
auto axis = D1 * (diff.x + diff.y);
// the savings from diagonal walks
auto diag = (D2 - 2 * D1) * util::min (diff);
return axis + diag;
}
//-------------------------------------------------------------------------
template <size_t S, typename T, typename U>
constexpr typename std::common_type<T,U>::type