Skip to content

Commit

Permalink
Updates UnitVec for improved performance & accuracy
Browse files Browse the repository at this point in the history
  • Loading branch information
louis-langholtz committed Oct 6, 2023
1 parent 26c995c commit 74d904c
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion Library/include/playrho/d2/UnitVec.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,18 @@ class UnitVec
static PolarCoord<T> Get(const T x, const T y,
const UnitVec& fallback = GetDefaultFallback()) noexcept
{
// Try the faster way first...
// Try the fastest way first...
enum { None = 0x0, Left = 0x1, Right = 0x2, Up = 0x4, Down = 0x8 };
switch (((x > 0)? Right: (x < 0) ? Left: 0) | ((y > 0)? Up: (y < 0)? Down: 0)) {
case Right: return std::make_pair(GetRight(), x);
case Left: return std::make_pair(GetLeft(), -x);
case Up: return std::make_pair(GetTop(), y);
case Down: return std::make_pair(GetBottom(), -y);
case None: return std::make_pair(fallback, T{});
default: break;
}

// Try the faster way next...
const auto magnitudeSquared = x * x + y * y;
if (isnormal(magnitudeSquared))
{
Expand Down

0 comments on commit 74d904c

Please sign in to comment.