#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Point3 {
pub x: f64,
pub y: f64,
pub z: f64,
}
impl Point3 {
pub fn new(x: f64, y: f64, z: f64) -> Self {
Self { x, y, z }
}
pub fn distance(&self, other: &Self) -> f64 {
((self.x - other.x).powi(2) + (self.y - other.y).powi(2) + (self.z - other.z).powi(2))
.sqrt()
}
pub fn cross(a: &Self, b: &Self) -> Self {
Self {
x: a.y * b.z - a.z * b.y,
y: a.z * b.x - a.x * b.z,
z: a.x * b.y - a.y * b.x,
}
}
pub fn dot(a: &Self, b: &Self) -> f64 {
a.x * b.x + a.y * b.y + a.z * b.z
}
pub fn sub(&self, other: &Self) -> Self {
Self {
x: self.x - other.x,
y: self.y - other.y,
z: self.z - other.z,
}
}
pub fn add(&self, other: &Self) -> Self {
Self {
x: self.x + other.x,
y: self.y + other.y,
z: self.z + other.z,
}
}
pub fn scale(&self, s: f64) -> Self {
Self {
x: self.x * s,
y: self.y * s,
z: self.z * s,
}
}
pub fn length(&self) -> f64 {
(self.x * self.x + self.y * self.y + self.z * self.z).sqrt()
}
pub fn normalized(&self) -> Self {
let len = self.length();
if len == 0.0 {
*self
} else {
self.scale(1.0 / len)
}
}
}
impl From<[f64; 3]> for Point3 {
fn from(value: [f64; 3]) -> Self {
Self::new(value[0], value[1], value[2])
}
}
impl From<Point3> for [f64; 3] {
fn from(value: Point3) -> Self {
[value.x, value.y, value.z]
}
}