Skip to content

Commit

Permalink
Add additional distance functions
Browse files Browse the repository at this point in the history
  • Loading branch information
Rasie1 committed Jul 7, 2022
1 parent e7a3cd0 commit 1996a06
Showing 1 changed file with 32 additions and 11 deletions.
43 changes: 32 additions & 11 deletions triangle.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,14 @@
#include <array>
#include <utility>
#include <cmath>
#include <algorithm>

namespace Triangle {

namespace DefaultConstants
{
const float Side = 173.20508075689f;
const float Height = 150.f;
const float HeightK = 0.86692549378f;
const float Center = 0.5773502f;
}
const float Side = 173.20508075689f;
const float Height = 150.f;
const float HeightK = 0.86692549378f;
const float Center = 0.5773502f;

struct Pos
{
Expand Down Expand Up @@ -49,8 +47,8 @@ inline Vec2 gridToWorld(
inline Vec2 gridToWorld(const Pos& pos)
{
return Vec2{
pos.x * DefaultConstants::Side + pos.y * DefaultConstants::Side / 2.f,
pos.y * DefaultConstants::Height
pos.x * Side + pos.y * Side / 2.f,
pos.y * Height
};
}

Expand All @@ -67,8 +65,8 @@ inline Pos worldToGrid(

inline Pos worldToGrid(const Vec2& pos)
{
float y = std::get<1>(pos) / DefaultConstants::Height;
float x = std::get<0>(pos) / DefaultConstants::Side - y / 2.f;
float y = std::get<1>(pos) / Height;
float x = std::get<0>(pos) / Side - y / 2.f;
bool s = (x - floor(x) + y - floor(y)) > 1.f;
return Pos{int(x), int(y), int(s)};
}
Expand Down Expand Up @@ -133,4 +131,27 @@ inline int distance(const Pos& a, const Pos& b)
return abs(dx) + abs(dy) + abs(dx + dy + ds);
}

inline int triangleDistanceDiagonal(const Pos& a, const Pos& b)
{
auto dx = a.x - b.x;
auto dy = a.y - b.y;
auto ds = a.s - b.s;
auto adx = abs(dx);
auto ady = abs(dy);
auto adz = abs(dx + dy + ds);
auto m = std::min({adx, ady, adz});
return adx + ady + adz - m;
}

inline int triangleDistanceFullDiagonal(const Pos& a, const Pos& b)
{
auto dx = a.x - b.x;
auto dy = a.y - b.y;
auto ds = a.s - b.s;
auto adx = abs(dx);
auto ady = abs(dy);
auto adz = abs(dx + dy + ds);
return std::max({adx, ady, adz});
}

}

0 comments on commit 1996a06

Please sign in to comment.