From f0879c0a0c75b9d800f62118d02021689351b205 Mon Sep 17 00:00:00 2001 From: antifallobst Date: Fri, 15 Dec 2023 21:48:10 +0100 Subject: [PATCH] feat: made Matrix3 more dynamic --- lithium-utils/src/lib.rs | 52 ++++++++++++++++++++++++---------------- 1 file changed, 32 insertions(+), 20 deletions(-) diff --git a/lithium-utils/src/lib.rs b/lithium-utils/src/lib.rs index 7556724..dc027ca 100644 --- a/lithium-utils/src/lib.rs +++ b/lithium-utils/src/lib.rs @@ -1,32 +1,28 @@ use std::fmt::{Debug, Formatter}; -use std::ops::{Add, Mul, Sub}; +use std::ops::{Add, Index, Mul, Sub}; -pub struct Matrix3([Vector3; C]); +pub struct Matrix3(Vec); -impl Debug for Matrix3<3> { +impl Debug for Matrix3 { fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { - writeln!( - f, - "[ {:03.2} {:03.2} {:03.2}", - self.0[0].x, self.0[1].x, self.0[2].x, - )?; - writeln!( - f, - " {:03.2} {:03.2} {:03.2}", - self.0[0].y, self.0[1].y, self.0[2].y, - )?; - writeln!( - f, - " {:03.2} {:03.2} {:03.2} ]", - self.0[0].z, self.0[1].z, self.0[2].z, - )?; + write!(f, "[")?; + for coord in 0..3 { + for vec in self.0.iter() { + write!(f, " {:06.2}", vec[coord])?; + } + if coord == 2 { + write!(f, " ]\n")?; + } else { + write!(f, "\n ")?; + } + } Ok(()) } } -impl Matrix3<3> { +impl Matrix3 { pub fn new(i: Vector3, j: Vector3, k: Vector3) -> Self { - Self { 0: [i, j, k] } + Self { 0: vec![i, j, k] } } pub fn transform(&self, vector: Vector3) -> Vector3 { @@ -69,6 +65,18 @@ impl Default for Vector3 { } } +impl Index for Vector3 { + type Output = f32; + fn index(&self, index: usize) -> &Self::Output { + match index { + 0 => &self.x, + 1 => &self.y, + 2 => &self.z, + _ => panic!("Index {index} is out of bounds in a Vector3!"), + } + } +} + impl Add for Vector3 { type Output = Self; fn add(self, rhs: Self) -> Self::Output { @@ -117,6 +125,10 @@ impl Vector3 { pub fn new(x: f32, y: f32, z: f32) -> Self { Self { x, y, z } } + + pub fn flatten(&self) -> Vector2 { + Vector2::new(self.x, self.y) + } } #[derive(Debug)]