use serde::{Deserialize, Serialize};
use std::ops::{Add, AddAssign, Index, IndexMut, Mul, Sub};
#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
pub struct Vector3D {
pub x: f64,
pub y: f64,
pub z: f64,
}
impl Vector3D {
pub fn new(x: f64, y: f64, z: f64) -> Self {
Self { x, y, z }
}
pub fn zero() -> Self {
Self { x: 0.0, y: 0.0, z: 0.0 }
}
pub fn distance(&self, other: &Vector3D) -> f64 {
((self.x - other.x).powi(2) +
(self.y - other.y).powi(2) +
(self.z - other.z).powi(2)).sqrt()
}
pub fn magnitude(&self) -> f64 {
(self.x * self.x + self.y * self.y + self.z * self.z).sqrt()
}
pub fn norm(&self) -> f64 {
self.magnitude()
}
pub fn zeros() -> Self {
Self { x: 0.0, y: 0.0, z: 0.0 }
}
pub fn dot(&self, other: &Vector3D) -> f64 {
self.x * other.x + self.y * other.y + self.z * other.z
}
pub fn iter(&self) -> impl Iterator<Item = f64> {
[self.x, self.y, self.z].into_iter()
}
}
impl std::ops::Add for Vector3D {
type Output = Vector3D;
fn add(self, other: Vector3D) -> Vector3D {
Vector3D {
x: self.x + other.x,
y: self.y + other.y,
z: self.z + other.z,
}
}
}
impl std::ops::Sub for Vector3D {
type Output = Vector3D;
fn sub(self, other: Vector3D) -> Vector3D {
Vector3D {
x: self.x - other.x,
y: self.y - other.y,
z: self.z - other.z,
}
}
}
impl std::ops::Mul<f64> for Vector3D {
type Output = Vector3D;
fn mul(self, scalar: f64) -> Vector3D {
Vector3D {
x: self.x * scalar,
y: self.y * scalar,
z: self.z * scalar,
}
}
}
impl std::ops::Mul<Vector3D> for f64 {
type Output = Vector3D;
fn mul(self, vector: Vector3D) -> Vector3D {
Vector3D {
x: self * vector.x,
y: self * vector.y,
z: self * vector.z,
}
}
}
impl std::ops::Neg for Vector3D {
type Output = Vector3D;
fn neg(self) -> Vector3D {
Vector3D {
x: -self.x,
y: -self.y,
z: -self.z,
}
}
}
impl std::ops::Div<f64> for Vector3D {
type Output = Vector3D;
fn div(self, scalar: f64) -> Vector3D {
Vector3D {
x: self.x / scalar,
y: self.y / scalar,
z: self.z / scalar,
}
}
}
impl Index<usize> for Vector3D {
type Output = f64;
fn index(&self, index: usize) -> &Self::Output {
match index {
0 => &self.x,
1 => &self.y,
2 => &self.z,
_ => panic!("Vector3D index out of bounds: {}", index),
}
}
}
impl IndexMut<usize> for Vector3D {
fn index_mut(&mut self, index: usize) -> &mut Self::Output {
match index {
0 => &mut self.x,
1 => &mut self.y,
2 => &mut self.z,
_ => panic!("Vector3D index out of bounds: {}", index),
}
}
}
impl AddAssign for Vector3D {
fn add_assign(&mut self, other: Vector3D) {
self.x += other.x;
self.y += other.y;
self.z += other.z;
}
}