From 74d904c837ba2304afe9164231fdeb3a9143a833 Mon Sep 17 00:00:00 2001 From: Louis Langholtz Date: Fri, 6 Oct 2023 08:07:03 -0600 Subject: [PATCH] Updates UnitVec for improved performance & accuracy --- Library/include/playrho/d2/UnitVec.hpp | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/Library/include/playrho/d2/UnitVec.hpp b/Library/include/playrho/d2/UnitVec.hpp index c19fefb71c..102cdb9555 100644 --- a/Library/include/playrho/d2/UnitVec.hpp +++ b/Library/include/playrho/d2/UnitVec.hpp @@ -126,7 +126,18 @@ class UnitVec static PolarCoord 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)) {