From c9489db97314cd86c7d05275ae7586fa8ac6a29a Mon Sep 17 00:00:00 2001 From: antifallobst Date: Sun, 17 Dec 2023 16:20:15 +0100 Subject: [PATCH] feat(utils): implemented math traits for Vector2 and added Vector4 --- lithium-utils/src/vector.rs | 147 ++++++++++++++++++++++++++++++++++-- 1 file changed, 142 insertions(+), 5 deletions(-) diff --git a/lithium-utils/src/vector.rs b/lithium-utils/src/vector.rs index 40e844d..2ed7bf9 100644 --- a/lithium-utils/src/vector.rs +++ b/lithium-utils/src/vector.rs @@ -1,6 +1,6 @@ use std::ops::{Add, Index, Mul, Sub}; -#[derive(Debug)] +#[derive(Debug, Copy, Clone)] pub struct Vector2 { pub x: f32, pub y: f32, @@ -8,7 +8,58 @@ pub struct Vector2 { impl Default for Vector2 { fn default() -> Self { - Self { x: 0_f32, y: 0_f32 } + Self { x: 0.0, y: 0.0 } + } +} + +impl Index for Vector2 { + type Output = f32; + fn index(&self, index: usize) -> &Self::Output { + match index { + 0 => &self.x, + 1 => &self.y, + _ => panic!("Index {index} is out of bounds in a Vector2!"), + } + } +} + +impl Add for Vector2 { + type Output = Self; + fn add(self, rhs: Self) -> Self::Output { + Self::Output { + x: self.x + rhs.x, + y: self.y + rhs.y, + } + } +} + +impl Sub for Vector2 { + type Output = Self; + fn sub(self, rhs: Self) -> Self::Output { + Self::Output { + x: self.x - rhs.x, + y: self.y - rhs.y, + } + } +} + +impl Mul for Vector2 { + type Output = Self; + fn mul(self, rhs: Self) -> Self::Output { + Self::Output { + x: self.x * rhs.x, + y: self.y * rhs.y, + } + } +} + +impl Mul for Vector2 { + type Output = Self; + fn mul(self, rhs: f32) -> Self::Output { + Self::Output { + x: self.x * rhs, + y: self.y * rhs, + } } } @@ -28,9 +79,9 @@ pub struct Vector3 { impl Default for Vector3 { fn default() -> Self { Self { - x: 0_f32, - y: 0_f32, - z: 0_f32, + x: 0.0, + y: 0.0, + z: 0.0, } } } @@ -96,3 +147,89 @@ impl Vector3 { Self { x, y, z } } } + +#[derive(Debug, Copy, Clone)] +pub struct Vector4 { + pub x: f32, + pub y: f32, + pub z: f32, + pub w: f32, +} + +impl Default for Vector4 { + fn default() -> Self { + Self { + x: 0.0, + y: 0.0, + z: 0.0, + w: 0.0, + } + } +} + +impl Index for Vector4 { + type Output = f32; + fn index(&self, index: usize) -> &Self::Output { + match index { + 0 => &self.x, + 1 => &self.y, + 2 => &self.z, + 3 => &self.w, + _ => panic!("Index {index} is out of bounds in a Vector4!"), + } + } +} + +impl Add for Vector4 { + type Output = Self; + fn add(self, rhs: Self) -> Self::Output { + Self::Output { + x: self.x + rhs.x, + y: self.y + rhs.y, + z: self.z + rhs.z, + w: self.w + rhs.w, + } + } +} + +impl Sub for Vector4 { + type Output = Self; + fn sub(self, rhs: Self) -> Self::Output { + Self::Output { + x: self.x - rhs.x, + y: self.y - rhs.y, + z: self.z - rhs.z, + w: self.w - rhs.w, + } + } +} + +impl Mul for Vector4 { + type Output = Self; + fn mul(self, rhs: Self) -> Self::Output { + Self::Output { + x: self.x * rhs.x, + y: self.y * rhs.y, + z: self.z * rhs.z, + w: self.w * rhs.w, + } + } +} + +impl Mul for Vector4 { + type Output = Self; + fn mul(self, rhs: f32) -> Self::Output { + Self::Output { + x: self.x * rhs, + y: self.y * rhs, + z: self.z * rhs, + w: self.w * rhs, + } + } +} + +impl Vector4 { + pub fn new(x: f32, y: f32, z: f32, w: f32) -> Self { + Self { x, y, z, w } + } +}