use point::Point; use std::ops::Add; use std::ops::Div; use std::ops::Mul; use std::ops::Neg; #[derive(Clone, Copy)] pub struct Vector3 { pub x: f32, pub y: f32, pub z: f32, } impl Vector3 { pub fn normalize(&self) -> Vector3 { self / self.length() } pub fn length_squared(&self) -> f32 { self.x * self.x + self.y * self.y + self.z * self.z } pub fn length(&self) -> f32 { self.length_squared().sqrt() } pub fn dot(&self, rhs: &Vector3) -> f32 { self.x * rhs.x + self.y * rhs.y + self.z * rhs.z } } impl<'a> Div for &'a Vector3 { type Output = Vector3; fn div(self, rhs: f32) -> Vector3 { Vector3 { x: self.x / rhs, y: self.y / rhs, z: self.z / rhs, } } } impl<'a> Mul for &'a Vector3 { type Output = Vector3; fn mul(self, rhs: f32) -> Vector3 { Vector3 { x: self.x * rhs, y: self.y * rhs, z: self.z * rhs, } } } impl Add for Vector3 { type Output = Vector3; fn add(self, rhs: Vector3) -> Vector3 { Vector3 { x: rhs.x + self.x, y: rhs.y + self.y, z: rhs.z + self.z, } } } impl Neg for Vector3 { type Output = Vector3; fn neg(self) -> Vector3 { Vector3 { x: -self.x, y: -self.y, z: -self.z, } } } impl From for Vector3 { fn from(p: Point) -> Self { Vector3 { x: p.x, y: p.y, z: p.z, } } }