firmath 0.3.4

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

#[derive(Copy, Clone)]
pub struct Vector3 {
    v: [f32; 3]
}

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

    pub fn get_vector(&self) -> &[f32; 3] {
        &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 length(&self) -> f32 {
        (self.x() * self.x() + self.y() * self.y() + self.z() * self.z()).sqrt()
    }

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

    pub fn cross(&self, v: &Vector3) -> Vector3 {
        Vector3::new(
            self.y() * v.z() - self.z() * v.y(),
            self.z() * v.x() - self.x() * v.z(),
            self.x() * v.y() - self.y() * v.x(),
        )
    }

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

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

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

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

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

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

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

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

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

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

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

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

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