summaryrefslogtreecommitdiffstats
path: root/vector
diff options
context:
space:
mode:
authorNao Pross <naopross@thearcway.org>2018-12-10 14:33:40 +0100
committerNao Pross <naopross@thearcway.org>2018-12-10 14:33:40 +0100
commit294d9eb9b312eeabde6865a50141e7e28256a911 (patch)
tree2d4eb87487fa90fd5b722e5a7f1c21517f83cefb /vector
parentUpdate makefile (diff)
downloadcplusplus-294d9eb9b312eeabde6865a50141e7e28256a911.tar.gz
cplusplus-294d9eb9b312eeabde6865a50141e7e28256a911.zip
Implement cross product for vec2 and vec3
Diffstat (limited to 'vector')
-rw-r--r--vector/vector.cpp26
1 files changed, 15 insertions, 11 deletions
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 <iostream>
+#include <iterator>
#include <cassert>
#include <array>
#include <initializer_list>
#include <algorithm>
template<typename T, std::size_t d>
-class basic_vec : public std::array<T, d> {
-public:
+struct basic_vec : public std::array<T, d> {
static constexpr std::size_t dimensions = d;
basic_vec();
- basic_vec(std::initializer_list<T> l);
- template<std::size_t n>
- basic_vec(const basic_vec<T, n>& other);
+ basic_vec(const std::initializer_list<T> l);
+ template<std::size_t n> basic_vec(const basic_vec<T, n>& other);
T length() const;
};
@@ -24,17 +23,20 @@ basic_vec<T, d>::basic_vec() : std::array<T, d>() {
}
template<typename T, std::size_t d>
-basic_vec<T, d>::basic_vec(std::initializer_list<T> l) {
+basic_vec<T, d>::basic_vec(const std::initializer_list<T> l) {
+ basic_vec();
+
+ // why can't this sh*t be a constexpr with static_assert???
assert(l.size() <= d);
- this->fill(static_cast<T>(0));
std::copy(l.begin(), l.end(), this->begin());
}
template<typename T, std::size_t d>
template<std::size_t n>
basic_vec<T, d>::basic_vec(const basic_vec<T, n>& other) {
+ basic_vec();
+
static_assert(d >= n);
- this->fill(static_cast<T>(0));
for (std::size_t i = 0; i < n; i++) {
this->at(i) = other.at(i);
}
@@ -122,7 +124,10 @@ public:
template<typename T>
vec3<T> vec3<T>::cross(const vec3<T>& rhs, const vec3<T>& lhs) {
vec3<T> 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<typename T>
vec3<T> vec2<T>::cross(const vec2<T>& rhs, const vec2<T>& lhs) {
- vec3<T> res;
- // TODO: cross product
+ return vec3<T>::cross(vec3<T>(rhs), vec3<T>(lhs));
}