mirror of
https://github.com/RGBCube/serenity
synced 2025-07-28 20:47:45 +00:00
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:
parent
d75135663b
commit
62ffe67a9f
1 changed files with 5 additions and 6 deletions
|
@ -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)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue