summary refs log tree commit diff
path: root/src/Math
diff options
context:
space:
mode:
Diffstat (limited to 'src/Math')
-rw-r--r--src/Math/Math.hpp3
-rw-r--r--src/Math/Matrix.hpp65
-rw-r--r--src/Math/Vector.hpp72
3 files changed, 135 insertions, 5 deletions
diff --git a/src/Math/Math.hpp b/src/Math/Math.hpp
index 66ffd36..8204728 100644
--- a/src/Math/Math.hpp
+++ b/src/Math/Math.hpp
@@ -1,3 +1,4 @@
 #pragma once
 
-#include "Vector.hpp"
\ No newline at end of file
+#include "Vector.hpp"
+#include "Matrix.hpp"
\ No newline at end of file
diff --git a/src/Math/Matrix.hpp b/src/Math/Matrix.hpp
new file mode 100644
index 0000000..adc34a9
--- /dev/null
+++ b/src/Math/Matrix.hpp
@@ -0,0 +1,65 @@
+#pragma once
+
+#include <sstream>
+#include <stddef.h>
+#include <iostream>
+
+template <size_t R, size_t C, typename T = float>
+struct Matrix {
+public:
+    Matrix<R, C, T>() : elements{} {};
+
+    template<typename ...Args>
+    explicit Matrix<R, C, T>(Args... args): elements{ args... } {};
+
+    Vector<C, T> row(size_t index) {
+        return { &elements[index * C] };
+    }
+
+    Vector<R, T> col(size_t index) {
+        Vector<R, T> result{};
+        for (int i = 0; i < R; i++) {
+            result[i] = this->operator()(index, i);
+        }
+        return result;
+    }
+
+    template<size_t N>
+    Matrix<R, N, T> operator*(Matrix<C, N, T> other) {
+        Matrix<R, N, T> result{};
+        for (int y = 0; y < R; y++) {
+            for (int x = 0; x < N; x++) {
+                auto r = row(y);
+                auto c = other.col(x);
+
+                auto dot = r * c;
+
+                std::cout << x << ", " << y << ": "
+                    << "(" << r.string() << ", " << c.string() << ")"
+                    << dot << std::endl;
+
+                result(x, y) = dot;
+            }
+        }
+        return result;
+    }
+
+    auto& operator()(size_t x, size_t y) {
+        return elements[y * C + x];
+    }
+
+    std::string string() {
+        std::stringstream str{};
+        for (int x = 0; x < R; x++) {
+            for (int y = 0; y < C; y++) {
+                str << this->operator()(x, y) << " ";
+            }
+
+            str << "\n";
+        }
+
+        return str.str();
+    }
+
+    T elements[R * C];
+};
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];
 };