astraeus 0.1.0

A library for space mission analysis
Documentation
//! Module for basic vector types

/// 3D vector in Cartesian coordinates.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct CartesianVector {
    /// The x-coordinate of the vector.
    pub x: f64,
    /// The y-coordinate of the vector.
    pub y: f64,
    /// The z-coordinate of the vector.
    pub z: f64,
}

impl CartesianVector {
    /// Create a new `CartesianVector`.
    ///
    /// # Example
    /// ## Code
    /// ```rust
    /// use astraeus::math::vectors::CartesianVector;
    ///
    /// let vector = CartesianVector::new(1.0, 2.0, 3.0);
    /// println!("{:?}", vector);
    /// ```
    /// ## Output
    /// ```bash
    /// CartesianVector { x: 1.0, y: 2.0, z: 3.0 }
    /// ```
    pub fn new(x: f64, y: f64, z: f64) -> Self {
        CartesianVector { x, y, z }
    }

    /// Calculate the magnitude of the vector.
    pub fn magnitude(&self) -> f64 {
        (self.x * self.x + self.y * self.y + self.z * self.z).sqrt()
    }

    /// Calculate the dot product of two vectors.
    pub fn dot(&self, other: &CartesianVector) -> f64 {
        self.x * other.x + self.y * other.y + self.z * other.z
    }

    /// Calculate the cross product of two vectors.
    pub fn cross(&self, other: &CartesianVector) -> CartesianVector {
        CartesianVector {
            x: self.y * other.z - self.z * other.y,
            y: self.z * other.x - self.x * other.z,
            z: self.x * other.y - self.y * other.x,
        }
    }

    /// Normalize the vector.
    pub fn normalize(&self) -> CartesianVector {
        let magnitude = self.magnitude();
        CartesianVector {
            x: self.x / magnitude,
            y: self.y / magnitude,
            z: self.z / magnitude,
        }
    }

    /// Add two vectors.
    pub fn add(&self, other: &CartesianVector) -> CartesianVector {
        CartesianVector {
            x: self.x + other.x,
            y: self.y + other.y,
            z: self.z + other.z,
        }
    }
}