diff --git a/src/math/point.rs b/src/math/point.rs index 1974176..f95c9e2 100644 --- a/src/math/point.rs +++ b/src/math/point.rs @@ -23,7 +23,6 @@ use std::cast; use math::{Dimensioned, SwapComponents}; -use math::{Mat2, Mat3, Quat}; use math::{Ray2, Ray3}; use math::{Vec2, ToVec2, AsVec2}; use math::{Vec3, ToVec3, AsVec3}; @@ -36,8 +35,10 @@ pub trait Point: Eq + Mul + ApproxEq + ToStr { - pub fn translate(&self, offset: &Vec) -> Self; - pub fn scale(&self, factor: &Vec) -> Self; + pub fn translate_v(&self, offset: &Vec) -> Self; + pub fn scale_s(&self, factor: T) -> Self; + pub fn scale_v(&self, factor: &Vec) -> Self; + pub fn displacement(&self, other: &Self) -> Vec; pub fn distance2(&self, other: &Self) -> T; pub fn distance(&self, other: &Self) -> T; pub fn direction(&self, other: &Self) -> Vec; @@ -102,33 +103,33 @@ impl ToVec3 for Point2 { } } -impl Point2 { - /// Rotates a point around the `z` axis using a scalar angle. - #[inline] - pub fn rotate_z(&self, radians: &T) -> Point2 { - Point2::new(self.x.cos() * (*radians), - self.y.sin() * (*radians)) - } - - /// Applies a rotation to the point using a rotation matrix. - #[inline] - pub fn rotate_m(&self, mat: &Mat2) -> Point2 { - Point2::from_vec2(mat.mul_v(self.as_vec2())) - } -} - impl Point, Ray2> for Point2 { /// Applies a displacement vector to the point. #[inline] - pub fn translate(&self, offset: &Vec2) -> Point2 { - (*self) + (*offset) + pub fn translate_v(&self, offset: &Vec2) -> Point2 { + Point2::new(self.x + offset.x, + self.y + offset.y) + } + + /// Scales the distance from the point to the origin by a scalar value. + #[inline] + pub fn scale_s(&self, factor: T) -> Point2 { + Point2::new(self.x * factor, + self.y * factor) } /// Scales the distance from the point to the origin using the components /// of a vector. #[inline] - pub fn scale(&self, factor: &Vec2) -> Point2 { - (*self) * (*factor) + pub fn scale_v(&self, factor: &Vec2) -> Point2 { + Point2::new(self.x * factor.x, + self.y * factor.y) + } + + /// Calculates the displacement required to move the point to `other`. + pub fn displacement(&self, other: &Point2) -> Vec2 { + Vec2::new(self.x - other.x, + self.y - other.y) } /// Returns the squared distance from the point to `other`. This does not @@ -159,28 +160,25 @@ impl Point, Ray2> for Point2 { } } -impl Add, Point2> for Point2 { +impl Add, Point2> for Point2 { /// Applies a displacement vector to the point. fn add(&self, offset: &Vec2) -> Point2 { - Point2::new(self.x + offset.x, - self.y + offset.y) + self.translate_v(offset) } } -impl Sub, Vec2> for Point2 { +impl Sub, Vec2> for Point2 { /// Calculates the displacement vector from the point to `other`. fn sub(&self, other: &Point2) -> Vec2 { - Vec2::new(self.x - other.x, - self.y - other.y) + self.displacement(other) } } -impl Mul, Point2> for Point2 { +impl Mul, Point2> for Point2 { /// Scales the distance from the point to the origin using the components /// of a vector. fn mul(&self, factor: &Vec2) -> Point2 { - Point2::new(self.x * factor.x, - self.y * factor.y) + self.scale_v(factor) } } @@ -259,32 +257,37 @@ impl ToVec4 for Point3 { } } -impl Point3 { - /// Applies a rotation to the point using a quaternion. - #[inline] - pub fn rotate_q(&self, quat: &Quat) -> Point3 { - Point3::from_vec3(quat.mul_v(self.as_vec3())) - } - - /// Applies a rotation to the point using a rotation matrix. - #[inline] - pub fn rotate_m(&self, mat: &Mat3) -> Point3 { - Point3::from_vec3(mat.mul_v(self.as_vec3())) - } -} - impl Point, Ray3> for Point3 { /// Applies a displacement vector to the point. #[inline] - pub fn translate(&self, offset: &Vec3) -> Point3 { - (*self) + (*offset) + pub fn translate_v(&self, offset: &Vec3) -> Point3 { + Point3::new(self.x + offset.x, + self.y + offset.y, + self.z + offset.z) + } + + /// Scales the distance from the point to the origin by a scalar value. + #[inline] + pub fn scale_s(&self, factor: T) -> Point3 { + Point3::new(self.x * factor, + self.y * factor, + self.z * factor) } /// Scales the distance from the point to the origin using the components /// of a vector. #[inline] - pub fn scale(&self, factor: &Vec3) -> Point3 { - (*self) * (*factor) + pub fn scale_v(&self, factor: &Vec3) -> Point3 { + Point3::new(self.x * factor.x, + self.y * factor.y, + self.z * factor.z) + } + + /// Calculates the displacement required to move the point to `other`. + pub fn displacement(&self, other: &Point3) -> Vec3 { + Vec3::new(self.x - other.x, + self.y - other.y, + self.z - other.z) } /// Returns the squared distance from the point to `other`. This does not @@ -315,31 +318,25 @@ impl Point, Ray3> for Point3 { } } -impl Add, Point3> for Point3 { +impl Add, Point3> for Point3 { /// Applies a displacement vector to the point fn add(&self, offset: &Vec3) -> Point3 { - Point3::new(self.x + offset.x, - self.y + offset.y, - self.z + offset.z) + self.translate_v(offset) } } -impl Sub, Vec3> for Point3 { +impl Sub, Vec3> for Point3 { /// Calculates the displacement required to move the point to `other`. fn sub(&self, other: &Point3) -> Vec3 { - Vec3::new(self.x - other.x, - self.y - other.y, - self.z - other.z) + self.displacement(other) } } -impl Mul, Point3> for Point3 { +impl Mul, Point3> for Point3 { /// Scales the distance from the point to the origin using the components /// of a vector. fn mul(&self, factor: &Vec3) -> Point3 { - Point3::new(self.x * factor.x, - self.y * factor.y, - self.z * factor.z) + self.scale_v(factor) } }