feat(utils): implemented math traits for Vector2 and added Vector4
This commit is contained in:
parent
0f32424c1b
commit
c9489db973
|
@ -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<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 {
|
||||
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<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 }
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue