#[derive(Copy,Clone,Debug)]
pub struct V3 {
pub x: f64,
pub y: f64,
pub z: f64
}
impl std::fmt::Display for V3 {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "[{}, {}, {}]", self.x, self.y, self.z)
}
}
impl std::ops::Mul<f64> for V3 {
type Output = V3;
fn mul(self, f: f64) -> Self::Output {
V3 {
x: self.x * f,
y: self.y * f,
z: self.z * f,
}
}
}
impl std::ops::MulAssign<f64> for V3 {
fn mul_assign(&mut self, f: f64) {
self.x *= f;
self.y *= f;
self.z *= f;
}
}
impl std::ops::Add<V3> for V3 {
type Output = V3;
fn add(self, rhs: V3) -> V3 {
V3 {
x: self.x + rhs.x,
y: self.y + rhs.y,
z: self.z + rhs.z,
}
}
}
impl std::ops::AddAssign<V3> for V3 {
fn add_assign(&mut self, other: Self) {
self.x += other.x;
self.y += other.y;
self.z += other.z;
}
}
impl std::ops::Neg for V3 {
type Output = Self;
fn neg(self) -> Self::Output {
V3 {
x: -self.x,
y: -self.y,
z: -self.z,
}
}
}
impl std::ops::Sub<V3> for V3 {
type Output = Self;
fn sub(self, other: Self) -> Self::Output {
V3 {
x: self.x - other.x,
y: self.y - other.y,
z: self.z - other.z,
}
}
}
impl std::ops::SubAssign<V3> for V3 {
fn sub_assign(&mut self, other: Self) {
self.x -= other.x;
self.y -= other.y;
self.z -= other.z;
}
}
impl std::ops::Div<f64> for V3 {
type Output = Self;
fn div(self, f: f64) -> Self::Output {
V3 {
x: self.x / f,
y: self.y / f,
z: self.z / f,
}
}
}
impl std::ops::DivAssign<f64> for V3 {
fn div_assign(&mut self, f: f64) {
self.x /= f;
self.y /= f;
self.z /= f;
}
}
impl V3 {
pub fn magnitude(self) -> f64 {
(self.x*self.x + self.y*self.y + self.z*self.z).sqrt()
}
pub fn normalize(self) -> Self {
self / self.magnitude()
}
pub fn x_product(self, other: Self) -> Self {
V3 {
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,
}
}
}