feat: made Matrix3 more dynamic

This commit is contained in:
antifallobst 2023-12-15 21:48:10 +01:00
parent c45357244a
commit f0879c0a0c
Signed by: antifallobst
GPG Key ID: 2B4F402172791BAF
1 changed files with 32 additions and 20 deletions

View File

@ -1,32 +1,28 @@
use std::fmt::{Debug, Formatter}; use std::fmt::{Debug, Formatter};
use std::ops::{Add, Mul, Sub}; use std::ops::{Add, Index, Mul, Sub};
pub struct Matrix3<const C: usize>([Vector3; C]); pub struct Matrix3(Vec<Vector3>);
impl Debug for Matrix3<3> { impl Debug for Matrix3 {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
writeln!( write!(f, "[")?;
f, for coord in 0..3 {
"[ {:03.2} {:03.2} {:03.2}", for vec in self.0.iter() {
self.0[0].x, self.0[1].x, self.0[2].x, write!(f, " {:06.2}", vec[coord])?;
)?; }
writeln!( if coord == 2 {
f, write!(f, " ]\n")?;
" {:03.2} {:03.2} {:03.2}", } else {
self.0[0].y, self.0[1].y, self.0[2].y, write!(f, "\n ")?;
)?; }
writeln!( }
f,
" {:03.2} {:03.2} {:03.2} ]",
self.0[0].z, self.0[1].z, self.0[2].z,
)?;
Ok(()) Ok(())
} }
} }
impl Matrix3<3> { impl Matrix3 {
pub fn new(i: Vector3, j: Vector3, k: Vector3) -> Self { 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 { pub fn transform(&self, vector: Vector3) -> Vector3 {
@ -69,6 +65,18 @@ impl Default for Vector3 {
} }
} }
impl Index<usize> 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 { impl Add for Vector3 {
type Output = Self; type Output = Self;
fn add(self, rhs: Self) -> Self::Output { fn add(self, rhs: Self) -> Self::Output {
@ -117,6 +125,10 @@ impl Vector3 {
pub fn new(x: f32, y: f32, z: f32) -> Self { pub fn new(x: f32, y: f32, z: f32) -> Self {
Self { x, y, z } Self { x, y, z }
} }
pub fn flatten(&self) -> Vector2 {
Vector2::new(self.x, self.y)
}
} }
#[derive(Debug)] #[derive(Debug)]