use traits::dot::Dot; use traits::sqrt::Sqrt; #[deriving(Eq)] pub struct Vec3 { x : T, y : T, z : T } pub fn Vec3(x: T, y: T, z: T) -> Vec3 { Vec3 {x: x, y: y, z: z} } impl> Add, Vec3> for Vec3 { fn add(&self, other: &Vec3) -> Vec3 { Vec3(self.x + other.x, self.y + other.y, self.z + other.z) } } impl> Sub, Vec3> for Vec3 { fn sub(&self, other: &Vec3) -> Vec3 { Vec3(self.x - other.x, self.y - other.y, self.z - other.z) } } impl ToStr for Vec3 { fn to_str(&self) -> ~str { ~"Vec3 " + "{ x : " + self.x.to_str() + ", y : " + self.y.to_str() + ", z : " + self.z.to_str() + " }" } } impl + Add + Sqrt> Dot for Vec3 { fn dot(&self, other : &Vec3) -> T { self.x * other.x + self.y * other.y + self.z * other.z } fn sqnorm(&self) -> T { self.dot(self) } fn norm(&self) -> T { self.sqnorm().sqrt() } }