From ea6cfa2eb0ad8aa392860f6f9171b79ef6f948d2 Mon Sep 17 00:00:00 2001 From: Lephenixnoir Date: Sat, 22 Jul 2023 17:29:06 +0200 Subject: [PATCH] libnum: add .dot() method to vec objects --- libnum/include/num/vec.h | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/libnum/include/num/vec.h b/libnum/include/num/vec.h index b95afe5..cd685b1 100644 --- a/libnum/include/num/vec.h +++ b/libnum/include/num/vec.h @@ -35,6 +35,13 @@ struct vec inline constexpr T const &operator[](int i) const { return ((T *)this)[i]; } + + constexpr T dot(vec const &other) const { + T r(0); + for(int i = 0; i < N; ++i) + r += coords[i] * other[i]; + return r; + } }; template @@ -53,6 +60,10 @@ struct vec inline constexpr T const &operator[](int i) const { return ((T *)this)[i]; } + + inline constexpr T dot(vec const &other) const { + return x * other.x + y * other.y; + } }; using vec2 = vec; @@ -85,6 +96,10 @@ struct vec inline constexpr vec yz() const { return vec(y, z); } + + inline constexpr T dot(vec const &other) const { + return x * other.x + y * other.y + z * other.z; + } }; using vec3 = vec; @@ -113,6 +128,10 @@ struct vec inline constexpr vec xyz() const { return vec(x, y, z); } + + constexpr T dot(vec const &other) const { + return x * other.x + y * other.y + z * other.z + w * other.w; + } }; using vec4 = vec;