From 294d9eb9b312eeabde6865a50141e7e28256a911 Mon Sep 17 00:00:00 2001 From: Nao Pross Date: Mon, 10 Dec 2018 14:33:40 +0100 Subject: Implement cross product for vec2 and vec3 --- vector/vector.cpp | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) (limited to 'vector') diff --git a/vector/vector.cpp b/vector/vector.cpp index 0322ee2..9e0ca6c 100644 --- a/vector/vector.cpp +++ b/vector/vector.cpp @@ -1,19 +1,18 @@ #include +#include #include #include #include #include template -class basic_vec : public std::array { -public: +struct basic_vec : public std::array { static constexpr std::size_t dimensions = d; basic_vec(); - basic_vec(std::initializer_list l); - template - basic_vec(const basic_vec& other); + basic_vec(const std::initializer_list l); + template basic_vec(const basic_vec& other); T length() const; }; @@ -24,17 +23,20 @@ basic_vec::basic_vec() : std::array() { } template -basic_vec::basic_vec(std::initializer_list l) { +basic_vec::basic_vec(const std::initializer_list l) { + basic_vec(); + + // why can't this sh*t be a constexpr with static_assert??? assert(l.size() <= d); - this->fill(static_cast(0)); std::copy(l.begin(), l.end(), this->begin()); } template template basic_vec::basic_vec(const basic_vec& other) { + basic_vec(); + static_assert(d >= n); - this->fill(static_cast(0)); for (std::size_t i = 0; i < n; i++) { this->at(i) = other.at(i); } @@ -122,7 +124,10 @@ public: template vec3 vec3::cross(const vec3& rhs, const vec3& lhs) { vec3 res; - // TODO: cross product + + res[0] = (rhs[1] * lhs[2]) - (rhs[2] * lhs[1]); + res[1] = (rhs[2] * lhs[0]) - (rhs[0] * lhs[2]); + res[2] = (rhs[0] * lhs[1]) - (rhs[1] * lhs[0]); return res; } @@ -142,8 +147,7 @@ public: template vec3 vec2::cross(const vec2& rhs, const vec2& lhs) { - vec3 res; - // TODO: cross product + return vec3::cross(vec3(rhs), vec3(lhs)); } -- cgit v1.2.1