#[derive(Debug, Clone, Copy, PartialEq)]
pub struct CartesianVector {
pub x: f64,
pub y: f64,
pub z: f64,
}
impl CartesianVector {
pub fn new(x: f64, y: f64, z: f64) -> Self {
CartesianVector { x, y, z }
}
pub fn magnitude(&self) -> f64 {
(self.x * self.x + self.y * self.y + self.z * self.z).sqrt()
}
pub fn dot(&self, other: &CartesianVector) -> f64 {
self.x * other.x + self.y * other.y + self.z * other.z
}
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,
}
}
pub fn normalize(&self) -> CartesianVector {
let magnitude = self.magnitude();
CartesianVector {
x: self.x / magnitude,
y: self.y / magnitude,
z: self.z / magnitude,
}
}
pub fn add(&self, other: &CartesianVector) -> CartesianVector {
CartesianVector {
x: self.x + other.x,
y: self.y + other.y,
z: self.z + other.z,
}
}
}