From e5b2bc8abbe041d56beb57b95cc444c20e526367 Mon Sep 17 00:00:00 2001 From: HampusM Date: Sat, 12 Sep 2026 12:50:44 +0200 Subject: feat(engine): add naive matrix multiplication fn --- engine/src/data_types/matrix.rs | 32 +++++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/engine/src/data_types/matrix.rs b/engine/src/data_types/matrix.rs index 3c54bb1..e28c73f 100644 --- a/engine/src/data_types/matrix.rs +++ b/engine/src/data_types/matrix.rs @@ -1,4 +1,4 @@ -use std::ops::{Index, IndexMut, Mul}; +use std::ops::{Add, Index, IndexMut, Mul}; use crate::data_types::dimens::Dimens3; use crate::vector::{Vec3, Vec4}; @@ -284,6 +284,36 @@ impl Mul for Matrix } } +impl + Mul> for Matrix +where + Value: Mul + Add + Default + Copy, +{ + type Output = Matrix; + + fn mul(self, rhs: Matrix) -> Self::Output + { + // https://en.wikipedia.org/wiki/Matrix_multiplication + + let mut out = Self::Output::new(); + + for (row_index, row) in self.items.iter().enumerate() { + for (column, item) in row.iter().enumerate() { + let rhs_items = &rhs.items[column]; + + for (rhs_item_index, rhs_item) in rhs_items.iter().enumerate() { + let prev = out[CellPos { row: row_index, col: rhs_item_index }]; + + out[CellPos { row: row_index, col: rhs_item_index }] = + prev + (*item * *rhs_item); + } + } + } + + out + } +} + impl Index for Matrix { -- cgit v1.2.3-18-g5258