LibGfx: Reimplement Vector::length() as a loop

This more generic loop supports arbitrary values of `N` and also gets
rid of that strange single argument `AK::hypot` invocation.
This commit is contained in:
Jelle Raaijmakers 2022-03-06 19:29:43 +01:00 committed by Andreas Kling
parent d75135663b
commit 62ffe67a9f
Notes: sideshowbarker 2024-07-17 17:50:14 +09:00

View file

@ -193,12 +193,11 @@ public:
[[nodiscard]] constexpr T length() const
{
if constexpr (N == 2)
return AK::hypot(m_data[0] * m_data[0] + m_data[1] * m_data[1]);
else if constexpr (N == 3)
return AK::sqrt(m_data[0] * m_data[0] + m_data[1] * m_data[1] + m_data[2] * m_data[2]);
else
return AK::sqrt(m_data[0] * m_data[0] + m_data[1] * m_data[1] + m_data[2] * m_data[2] + m_data[3] * m_data[3]);
T squared_sum {};
UNROLL_LOOP
for (auto i = 0u; i < N; ++i)
squared_sum += m_data[i] * m_data[i];
return AK::sqrt(squared_sum);
}
[[nodiscard]] constexpr VectorN<2, T> xy() const requires(N >= 3)