From 5ccae7781dd7834de44fce1254416e0d9b92b30b Mon Sep 17 00:00:00 2001 From: craftablescience Date: Sat, 3 Aug 2024 16:18:36 -0400 Subject: [PATCH] feat(sourcepp): add more vector methods --- include/sourcepp/math/Vector.h | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/include/sourcepp/math/Vector.h b/include/sourcepp/math/Vector.h index 6b793b695..c74e4f799 100644 --- a/include/sourcepp/math/Vector.h +++ b/include/sourcepp/math/Vector.h @@ -186,6 +186,39 @@ struct Vec { return out; } + [[nodiscard]] constexpr float magf() const { + float out = 0.0; + for (uint8_t i = 0; i < S; i++) { + out += std::pow((*this)[i], 2); + } + return std::sqrt(out); + } + + [[nodiscard]] constexpr double mag() const { + double out = 0.0; + for (uint8_t i = 0; i < S; i++) { + out += std::pow((*this)[i], 2); + } + return std::sqrt(out); + } + + [[nodiscard]] constexpr P sum() const { + P out{}; + for (uint8_t i = 0; i < S; i++) { + out += (*this)[i]; + } + return out; + } + + template + [[nodiscard]] constexpr P dot(const Vec& other) const { + Vec out; + for (uint8_t i = 0; i < S; i++) { + out[i] = (*this)[i] * static_cast

(other[i]); + } + return out.sum(); + } + [[nodiscard]] constexpr Vec abs() const { auto out = *this; for (uint8_t i = 0; i < S; i++) {