Rename Vector::length to Vector::magnitude

This commit is contained in:
Brendan Zabarauskas 2016-03-27 16:17:11 +11:00
parent bf4637352e
commit f7bc6dcc54
3 changed files with 30 additions and 29 deletions

View file

@ -83,7 +83,7 @@ impl<S: BaseFloat> Quaternion<S> {
/// calculated.
#[inline]
pub fn magnitude2(self) -> S {
self.s * self.s + self.v.length2()
self.s * self.s + self.v.magnitude2()
}
/// The magnitude of the quaternion

View file

@ -490,17 +490,19 @@ pub trait EuclideanVector: Vector + Sized where
self.dot(other).approx_eq(&Self::Scalar::zero())
}
/// Returns the squared length of the vector. This does not perform an
/// expensive square root operation like in the `length` method and can
/// therefore be more efficient for comparing the lengths of two vectors.
/// Returns the squared magnitude of the vector.
///
/// This does not perform an expensive square root operation like in
/// `Vector::magnitude` method, and so can be used to compare vectors more
/// efficiently.
#[inline]
fn length2(self) -> Self::Scalar {
fn magnitude2(self) -> Self::Scalar {
self.dot(self)
}
/// The norm of the vector.
/// The distance from the tail to the tip of the vector.
#[inline]
fn length(self) -> Self::Scalar {
fn magnitude(self) -> Self::Scalar {
// FIXME: Not sure why this annotation is needed
<<Self as Vector>::Scalar as ::rust_num::Float>::sqrt(self.dot(self))
}
@ -508,23 +510,22 @@ pub trait EuclideanVector: Vector + Sized where
/// The angle between the vector and `other`, in radians.
fn angle(self, other: Self) -> Rad<Self::Scalar>;
/// Returns a vector with the same direction, but with a `length` (or
/// `norm`) of `1`.
/// Returns a vector with the same direction, but with a magnitude of `1`.
#[inline]
#[must_use]
fn normalize(self) -> Self {
self.normalize_to(Self::Scalar::one())
}
/// Returns a vector with the same direction and a given `length`.
/// Returns a vector with the same direction and a given magnitude.
#[inline]
#[must_use]
fn normalize_to(self, length: Self::Scalar) -> Self {
self * (length / self.length())
fn normalize_to(self, magnitude: Self::Scalar) -> Self {
self * (magnitude / self.magnitude())
}
/// Returns the result of linarly interpolating the length of the vector
/// towards the length of `other` by the specified amount.
/// Returns the result of linearly interpolating the magnitude of the vector
/// towards the magnitude of `other` by the specified amount.
#[inline]
#[must_use]
fn lerp(self, other: Self, amount: Self::Scalar) -> Self {
@ -542,14 +543,14 @@ impl<S: BaseFloat> EuclideanVector for Vector2<S> {
impl<S: BaseFloat> EuclideanVector for Vector3<S> {
#[inline]
fn angle(self, other: Vector3<S>) -> Rad<S> {
Rad::atan2(self.cross(other).length(), self.dot(other))
Rad::atan2(self.cross(other).magnitude(), self.dot(other))
}
}
impl<S: BaseFloat> EuclideanVector for Vector4<S> {
#[inline]
fn angle(self, other: Vector4<S>) -> Rad<S> {
Rad::acos(self.dot(other) / (self.length() * other.length()))
Rad::acos(self.dot(other) / (self.magnitude() * other.magnitude()))
}
}

View file

@ -216,7 +216,7 @@ fn test_is_perpendicular() {
}
#[cfg(test)]
mod test_length {
mod test_magnitude {
use cgmath::*;
#[test]
@ -224,11 +224,11 @@ mod test_length {
let (a, a_res) = (Vector2::new(3.0f64, 4.0f64), 5.0f64); // (3, 4, 5) Pythagorean triple
let (b, b_res) = (Vector2::new(5.0f64, 12.0f64), 13.0f64); // (5, 12, 13) Pythagorean triple
assert_eq!(a.length2(), a_res * a_res);
assert_eq!(b.length2(), b_res * b_res);
assert_eq!(a.magnitude2(), a_res * a_res);
assert_eq!(b.magnitude2(), b_res * b_res);
assert_eq!(a.length(), a_res);
assert_eq!(b.length(), b_res);
assert_eq!(a.magnitude(), a_res);
assert_eq!(b.magnitude(), b_res);
}
#[test]
@ -236,11 +236,11 @@ mod test_length {
let (a, a_res) = (Vector3::new(2.0f64, 3.0f64, 6.0f64), 7.0f64); // (2, 3, 6, 7) Pythagorean quadruple
let (b, b_res) = (Vector3::new(1.0f64, 4.0f64, 8.0f64), 9.0f64); // (1, 4, 8, 9) Pythagorean quadruple
assert_eq!(a.length2(), a_res * a_res);
assert_eq!(b.length2(), b_res * b_res);
assert_eq!(a.magnitude2(), a_res * a_res);
assert_eq!(b.magnitude2(), b_res * b_res);
assert_eq!(a.length(), a_res);
assert_eq!(b.length(), b_res);
assert_eq!(a.magnitude(), a_res);
assert_eq!(b.magnitude(), b_res);
}
#[test]
@ -248,11 +248,11 @@ mod test_length {
let (a, a_res) = (Vector4::new(1.0f64, 2.0f64, 4.0f64, 10.0f64), 11.0f64); // (1, 2, 4, 10, 11) Pythagorean quintuple
let (b, b_res) = (Vector4::new(1.0f64, 2.0f64, 8.0f64, 10.0f64), 13.0f64); // (1, 2, 8, 10, 13) Pythagorean quintuple
assert_eq!(a.length2(), a_res * a_res);
assert_eq!(b.length2(), b_res * b_res);
assert_eq!(a.magnitude2(), a_res * a_res);
assert_eq!(b.magnitude2(), b_res * b_res);
assert_eq!(a.length(), a_res);
assert_eq!(b.length(), b_res);
assert_eq!(a.magnitude(), a_res);
assert_eq!(b.magnitude(), b_res);
}
}