use std::ops::{Add, Mul, Sub};
#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub struct TyVector3<T = f64> {
pub x: T,
pub y: T,
pub z: T,
}
impl<T> TyVector3<T> {
pub fn new(x: T, y: T, z: T) -> Self {
Self { x, y, z }
}
}
impl<T: Add<Output = T> + Copy + Mul<Output = T> + Sub<Output = T>> TyVector3<T> {
pub fn cross(&self, other: &Self) -> Self {
Self {
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 dot(&self, other: &Self) -> T {
self.x * other.x + self.y * other.y + self.z * other.z
}
}
impl<T: Add<Output = T>> Add for TyVector3<T> {
type Output = Self;
fn add(self, rhs: Self) -> Self {
Self {
x: self.x + rhs.x,
y: self.y + rhs.y,
z: self.z + rhs.z,
}
}
}
impl<T: Sub<Output = T>> Sub for TyVector3<T> {
type Output = Self;
fn sub(self, rhs: Self) -> Self {
Self {
x: self.x - rhs.x,
y: self.y - rhs.y,
z: self.z - rhs.z,
}
}
}
impl<T: Copy + Mul<Output = T>> Mul<T> for TyVector3<T> {
type Output = Self;
fn mul(self, rhs: T) -> Self {
Self {
x: self.x * rhs,
y: self.y * rhs,
z: self.z * rhs,
}
}
}
macro_rules! impl_ty_vector3_float {
($t:ty) => {
impl TyVector3<$t> {
pub fn magnitude(&self) -> $t {
(self.x * self.x + self.y * self.y + self.z * self.z).sqrt()
}
}
impl Mul<TyVector3<$t>> for $t {
type Output = TyVector3<$t>;
fn mul(self, rhs: TyVector3<$t>) -> TyVector3<$t> {
TyVector3 {
x: self * rhs.x,
y: self * rhs.y,
z: self * rhs.z,
}
}
}
};
}
impl_ty_vector3_float!(f32);
impl_ty_vector3_float!(f64);