use std::ops::{Add, Div, Mul, Sub}; use std::ops::{Index, IndexMut}; pub struct Vec3 { pub x: T, pub y: T, pub z: T, } impl> Add for Vec3 { type Output = Self; fn add(self, other: Self) -> Self { Self { x: self.x + other.x, y: self.y + other.y, z: self.z + other.z, } } } impl> Sub for Vec3 { type Output = Self; fn sub(self, other: Self) -> Self { Self { x: self.x - other.x, y: self.y - other.y, z: self.z - other.z, } } } impl, U: Clone> Mul for Vec3 { type Output = Self; fn mul(self, other: U) -> Self { Self { x: self.x * other.clone(), y: self.y * other.clone(), z: self.z * other.clone(), } } } impl, U: Clone> Div for Vec3 { type Output = Self; fn div(self, other: U) -> Self { Self { x: self.x / other.clone(), y: self.y / other.clone(), z: self.z / other.clone(), } } } impl + Add> Dot for Vec3 { fn dot(me: Self, other: Self) -> T { me.x * other.x + me.y * other.y + me.z * other.z } } impl + Sub> Cross for Vec3 { fn cross(me: Self, other: Self) -> Self { Self { x: me.y.clone() * other.z.clone() - me.z.clone() * other.y.clone(), y: me.z.clone() * other.x.clone() - me.x.clone() * other.z.clone(), z: me.x.clone() * other.y.clone() - me.y.clone() * other.x.clone(), } } } impl Index for Vec3 { type Output = T; fn index(&self, index: usize) -> &T { match index { 0 => &self.x, 1 => &self.y, 2 => &self.z, _ => panic!("Index not found"), } } } impl IndexMut for Vec3 { fn index_mut(&mut self, index: usize) -> &mut T { match index { 0 => &mut self.x, 1 => &mut self.y, 2 => &mut self.z, _ => panic!("Index not found"), } } } impl From<[T; 3]> for Vec3 { fn from(array: [T; 3]) -> Self { Self { x: array[0].clone(), y: array[1].clone(), z: array[2].clone(), } } } impl Into<[T; 3]> for Vec3 { fn into(self) -> [T; 3] { [self.x, self.y, self.z] } } pub trait Dot { fn dot(me: Self, other: Self) -> T; } pub trait Cross { fn cross(me: Self, other: Self) -> Self; }