diff options
| author | Mel <einebeere@gmail.com> | 2022-10-02 04:34:18 +0200 |
|---|---|---|
| committer | Mel <einebeere@gmail.com> | 2022-10-02 04:34:18 +0200 |
| commit | 3b289a2f75b6e96735519a65d93b6babd1b1759f (patch) | |
| tree | 27cfffa0d8fe85b96f380362ab0a2ffc367a1a6b /src/Math/Vector.hpp | |
| parent | 46ab7c6c8af19dcf537cab25aa468f4afc403940 (diff) | |
| download | meowcraft-3b289a2f75b6e96735519a65d93b6babd1b1759f.tar.zst meowcraft-3b289a2f75b6e96735519a65d93b6babd1b1759f.zip | |
Expand Vector and Matrix classes
Diffstat (limited to 'src/Math/Vector.hpp')
| -rw-r--r-- | src/Math/Vector.hpp | 72 |
1 files changed, 68 insertions, 4 deletions
diff --git a/src/Math/Vector.hpp b/src/Math/Vector.hpp index 9c76758..0145b58 100644 --- a/src/Math/Vector.hpp +++ b/src/Math/Vector.hpp @@ -1,9 +1,73 @@ #pragma once -struct Vector3 { +#include <stddef.h> +#include <sstream> +#include <iostream> +#include <iterator> + +template <size_t S, typename T = float> +struct Vector { public: - Vector3(float x, float y, float z) - : x(x), y(y), z(z) {}; + Vector(): elements{} {}; + + template<typename ...Args> + Vector<S, T>(Args... args) : elements{ args... } {}; + + Vector<S, T>(T values[S]) { + std::copy(values, values + S, elements); + }; + + T& operator[](size_t index) { + return elements[index]; + } + + Vector<S, T> operator*(T scalar) const { + Vector<S, T> result; + for (size_t index; index < S; index++) { + result = this[index] * scalar; + } + return result; + } + + T operator*(Vector<S, T> other) const { + T result = 0; + for (size_t index = 0; index < S; index++) { + result += this->elements[index] * other[index]; + } + return result; + } + + T x() { + static_assert(S > 0); + return elements[0]; + } + + T y() { + static_assert(S > 1); + return elements[1]; + } + + T z() { + static_assert(S > 2); + return elements[2]; + } + + T w() { + static_assert(S > 3); + return elements[3]; + } + + std::string string() { + std::stringstream str{}; + + str << "[ "; + for (int i = 0; i < S; i++) { + str << elements[i] << " "; + } + str << "]"; + + return str.str(); + } - float x, y, z; + T elements[S]; }; |
