feat(utils): implemented math traits for Vector2 and added Vector4

This commit is contained in:
antifallobst 2023-12-17 16:20:15 +01:00
parent 0f32424c1b
commit c9489db973
Signed by: antifallobst
GPG Key ID: 2B4F402172791BAF
1 changed files with 142 additions and 5 deletions

View File

@ -1,6 +1,6 @@
use std::ops::{Add, Index, Mul, Sub}; use std::ops::{Add, Index, Mul, Sub};
#[derive(Debug)] #[derive(Debug, Copy, Clone)]
pub struct Vector2 { pub struct Vector2 {
pub x: f32, pub x: f32,
pub y: f32, pub y: f32,
@ -8,7 +8,58 @@ pub struct Vector2 {
impl Default for Vector2 { impl Default for Vector2 {
fn default() -> Self { fn default() -> Self {
Self { x: 0_f32, y: 0_f32 } Self { x: 0.0, y: 0.0 }
}
}
impl Index<usize> 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<f32> 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 { impl Default for Vector3 {
fn default() -> Self { fn default() -> Self {
Self { Self {
x: 0_f32, x: 0.0,
y: 0_f32, y: 0.0,
z: 0_f32, z: 0.0,
} }
} }
} }
@ -96,3 +147,89 @@ impl Vector3 {
Self { x, y, z } 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<usize> 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<f32> 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 }
}
}