summary refs log tree commit diff
path: root/src/Math/Matrix.hpp
diff options
context:
space:
mode:
authorMel <einebeere@gmail.com>2022-10-02 04:34:18 +0200
committerMel <einebeere@gmail.com>2022-10-02 04:34:18 +0200
commit3b289a2f75b6e96735519a65d93b6babd1b1759f (patch)
tree27cfffa0d8fe85b96f380362ab0a2ffc367a1a6b /src/Math/Matrix.hpp
parent46ab7c6c8af19dcf537cab25aa468f4afc403940 (diff)
downloadmeowcraft-3b289a2f75b6e96735519a65d93b6babd1b1759f.tar.zst
meowcraft-3b289a2f75b6e96735519a65d93b6babd1b1759f.zip
Expand Vector and Matrix classes
Diffstat (limited to 'src/Math/Matrix.hpp')
-rw-r--r--src/Math/Matrix.hpp65
1 files changed, 65 insertions, 0 deletions
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];
+};