From e0cee0d34485a7945735c354d1d5d088cd2c3187 Mon Sep 17 00:00:00 2001 From: Brett Date: Wed, 8 Mar 2023 23:49:57 -0500 Subject: [PATCH] normalize --- include/blt/math/vectors.h | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/include/blt/math/vectors.h b/include/blt/math/vectors.h index 507ae68..a20f90c 100644 --- a/include/blt/math/vectors.h +++ b/include/blt/math/vectors.h @@ -56,6 +56,17 @@ namespace blt { return elements[3]; } + [[nodiscard]] inline T magnitude() const { + T total = 0; + for (int i = 0; i < size; i++) + total += elements[i] * elements[i]; + return std::sqrt(total); + } + + [[nodiscard]] inline vec normalize() const { + return *this / this->magnitude(); + } + inline T& operator[](int index) { return elements[index]; }