some_math_lib 0.1.2

A basic math library.
Documentation

use std::ops::{Add, Sub, Mul, Div};
use super::vector3::Vector3;

/// A 2-dimensional vector.
#[derive(Debug, Copy, Clone)]
pub struct Vector2 {
    pub x: f64,
    pub y: f64,
}

impl Vector2 {
    /// Creates a new vector with components ```x``` and ```y```.
    pub fn new(x: f64, y: f64) -> Self {
        Vector2 { x, y }
    }

    /// Returns the squared magnitude of the vector.
    pub fn squared_magnitude(&self) -> f64 {
        self.x.powi(2) + self.y.powi(2)
    }

    /// Returns the magnitude of the vector.
    pub fn magnitude(&self) -> f64 {
        self.squared_magnitude().sqrt()
    }

    /// Returns the dot product of ```self``` and ```rhs```.
    pub fn dot_product(&self, rhs: &Self) -> f64 {
        (self.x * rhs.x) + (self.y * rhs.y)
    }

    /// Returns the magnitude of the vector formed by the cross product of 
    /// ```self``` and ```rhs```.
    pub fn cross_product_magnitude(&self, &rhs: &Self) -> f64 {
        (self.x * rhs.y) - (self.y * rhs.x)
    }

    /// Returns a ```Vector3``` with an ```x``` and ```y``` of 
    /// ```0.0``` and a ```z``` of the computed magnitude.
    pub fn cross_product(&self, rhs: &Self) -> Vector3 {
        Vector3 {
            x: 0.0, y: 0.0,
            z: self.cross_product_magnitude(rhs)
        }
    }
}

impl Add for Vector2 {
    type Output = Self;

    fn add(self, rhs: Self) -> Self::Output {
        Vector2 {
            x: self.x + rhs.x,
            y: self.y + rhs.y,
        }
    }
}
impl Sub for Vector2 {
    type Output = Self;

    fn sub(self, rhs: Self) -> Self::Output {
        Vector2 {
            x: self.x - rhs.x,
            y: self.y - rhs.y,
        }
    }
}
impl Mul<f64> for Vector2 {
    type Output = Self;

    fn mul(self, scalar: f64) -> Self::Output {
        Vector2 {
            x: self.x * scalar,
            y: self.y * scalar,
        }
    }
}
impl Div<f64> for Vector2 {
    type Output = Self;

    fn div(self, scalar: f64) -> Self::Output {
        Vector2 {
            x: self.x / scalar,
            y: self.y / scalar,
        }
    }
}