firmath 0.4.2

Math Library for Graphics
Documentation
use crate::conversion::Conversion;

#[repr(C)]
#[derive(Copy, Clone)]
pub struct Vector4 {
    v: [f32; 4]
}

impl Vector4 {
    pub fn new(x: f32, y: f32, z: f32, w: f32) -> Vector4 {
        Self {
            v: [
                x, y, z, w
            ]
        }
    }

    pub fn get_vector(&self) -> [f32; 4] {
        self.v
    }

    pub fn get_vector_mut(&mut self) -> &mut [f32; 4] {
        &mut self.v
    }

    pub fn x(&self) -> f32 {
        self.v[0]
    }

    pub fn y(&self) -> f32 {
        self.v[1]
    }

    pub fn z(&self) -> f32 {
        self.v[2]
    }

    pub fn w(&self) -> f32 {
        self.v[3]
    }

    pub fn length(&self) -> f32 {
        (self.x() * self.x() + self.y() * self.y() + self.z() * self.z()).sqrt()
    }

    pub fn dot(&self, v: &Vector4) -> f32 {
        self.x() * v.x() + self.y() * v.y() + self.z() * v.z() + self.w() * v.w()
    }

    pub fn normalize(&self) -> Vector4 {
        if self.length() <= 0.0 {
            return 0.0.convert()
        }
        return Vector4::new(
            self.x() / self.length(),
            self.y() / self.length(),
            self.z() / self.length(),
            self.w() / self.length()
        )
    }
}

impl std::ops::Add for Vector4 {
    type Output = Self;

    fn add(self, rhs: Self) -> Self::Output {
        Self {
            v: [
                self.x() + rhs.x(),
                self.y() + rhs.y(),
                self.z() + rhs.z(),
                self.w() + rhs.w(),
            ]
        } 
    }
}

impl std::ops::AddAssign for Vector4 {
    fn add_assign(&mut self, rhs: Self) {
        *self = Self {
            v: [
                self.x() + rhs.x(),
                self.y() + rhs.y(),
                self.z() + rhs.z(),
                self.w() + rhs.w()
            ]
        }
    }
}

impl std::ops::Sub for Vector4 {
    type Output = Self;

    fn sub(self, rhs: Self) -> Self::Output {
        Self {
            v: [
                self.x() - rhs.x(),
                self.y() - rhs.y(),
                self.z() - rhs.z(),
                self.w() - rhs.w()
            ]
        } 
    }
}

impl std::ops::SubAssign for Vector4 {
    fn sub_assign(&mut self, rhs: Self) {
        *self = Self {
            v: [
                self.x() - rhs.x(),
                self.y() - rhs.y(),
                self.z() - rhs.z(),
                self.w() - rhs.w()
            ]
        }
    }
}

impl std::ops::Mul for Vector4 {
    type Output = Self;

    fn mul(self, rhs: Self) -> Self::Output {
        Self {
            v: [
                self.x() * rhs.x(),
                self.y() * rhs.y(),
                self.z() * rhs.z(),
                self.w() * rhs.w()
            ]
        } 
    }
}

impl std::ops::MulAssign for Vector4 {
    fn mul_assign(&mut self, rhs: Self) {
        *self = Self {
            v: [
                self.x() * rhs.x(),
                self.y() * rhs.y(),
                self.z() * rhs.z(),
                self.w() * rhs.w()
            ]
        }
    }
}

impl std::ops::Div for Vector4 {
    type Output = Self;

    fn div(self, rhs: Self) -> Self::Output {
        Self {
            v: [
                self.x() / rhs.x(),
                self.y() / rhs.y(),
                self.z() / rhs.z(),
                self.w() / rhs.w()
            ]
        } 
    }
}

impl std::ops::DivAssign for Vector4 {
    fn div_assign(&mut self, rhs: Self) {
        *self = Self {
            v: [
                self.x() / rhs.x(),
                self.y() / rhs.y(),
                self.z() / rhs.z(),
                self.w() / rhs.w()
            ]
        }
    }
}