Merge pull request #109 from atheriel/rot-fix

Fix `Matrix2::from_angle()` invocation and add a test for the feature.
This commit is contained in:
Brendan Zabarauskas 2014-08-06 22:03:26 +10:00
commit 38364a6439
3 changed files with 19 additions and 3 deletions

View file

@ -90,8 +90,8 @@ impl<S: BaseFloat> Matrix2<S> {
let cos_theta = cos(theta.clone()); let cos_theta = cos(theta.clone());
let sin_theta = sin(theta.clone()); let sin_theta = sin(theta.clone());
Matrix2::new(cos_theta.clone(), -sin_theta.clone(), Matrix2::new(cos_theta.clone(), sin_theta.clone(),
sin_theta.clone(), cos_theta.clone()) -sin_theta.clone(), cos_theta.clone())
} }
} }

View file

@ -149,7 +149,7 @@ pub trait Rotation3<S: BaseNum>: Rotation<S, Vector3<S>, Point3<S>>
/// ///
/// // Since sin(π/2) may not be exactly zero due to rounding errors, we can /// // Since sin(π/2) may not be exactly zero due to rounding errors, we can
/// // use cgmath's approx_eq() feature to show that it is close enough. /// // use cgmath's approx_eq() feature to show that it is close enough.
/// assert!(unit_y.approx_eq(&-Vector2::unit_y())); /// assert!(unit_y.approx_eq(&Vector2::unit_y()));
/// ///
/// // This is exactly equivalent to using the raw matrix itself: /// // This is exactly equivalent to using the raw matrix itself:
/// let unit_y2 = rot.to_matrix2().mul_v(&unit_x); /// let unit_y2 = rot.to_matrix2().mul_v(&unit_x);

View file

@ -15,6 +15,7 @@
use cgmath::matrix::*; use cgmath::matrix::*;
use cgmath::vector::*; use cgmath::vector::*;
use cgmath::angle::rad;
use cgmath::approx::ApproxEq; use cgmath::approx::ApproxEq;
pub mod matrix2 { pub mod matrix2 {
@ -395,3 +396,18 @@ fn test_predicates() {
assert!(Matrix4::from_value(6.0f64).is_diagonal()); assert!(Matrix4::from_value(6.0f64).is_diagonal());
} }
#[test]
fn test_from_angle() {
// Rotate the vector (1, 0) by π/2 radians to the vector (0, 1)
let rot1 = Matrix2::from_angle(rad(0.5f64 * Float::pi()));
assert!(rot1.mul_v(&Vector2::unit_x()).approx_eq(&Vector2::unit_y()));
// Rotate the vector (-1, 0) by -π/2 radians to the vector (0, 1)
let rot2 = -rot1;
assert!(rot2.mul_v(&-Vector2::unit_x()).approx_eq(&Vector2::unit_y()));
// Rotate the vector (1, 1) by π radians to the vector (-1, -1)
let rot3: Matrix2<f64> = Matrix2::from_angle(rad(Float::pi()));
assert!(rot3.mul_v(&Vector2::new(1.0, 1.0)).approx_eq(&Vector2::new(-1.0, -1.0)));
}