matrices: moving another doctest to unit
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Bill Thiede 2021-07-30 21:59:40 -07:00
parent c882fc81e5
commit 9befbd9ad2

View File

@ -140,38 +140,6 @@ impl PartialEq for Matrix3x3 {
/// Matrix4x4 represents a 4x4 matrix in row-major form. So, element `m[i][j]` corresponds to m<sub>i,j</sub>
/// where `i` is the row number and `j` is the column number.
///
/// # Examples
/// ```
/// use rtchallenge::{
/// float::consts::PI,
/// matrices::Matrix4x4,
/// tuples::{point, vector},
/// };
///
/// // Individual transformations are applied in sequence.
/// let p = point(1., 0., 1.);
/// let a = Matrix4x4::rotation_x(PI / 2.);
/// let b = Matrix4x4::scaling(5., 5., 5.);
/// let c = Matrix4x4::translation(10., 5., 7.);
/// // Apply rotation first.
/// let p2 = a * p;
/// assert_eq!(p2, point(1., -1., 0.));
/// // Then apply scaling.
/// let p3 = b * p2;
/// assert_eq!(p3, point(5., -5., 0.));
/// // Then apply translation.
/// let p4 = c * p3;
/// assert_eq!(p4, point(15., 0., 7.));
///
/// // Chained transformations must be applied in reverse order.
/// let p = point(1., 0., 1.);
/// let a = Matrix4x4::rotation_x(PI / 2.);
/// let b = Matrix4x4::scaling(5., 5., 5.);
/// let c = Matrix4x4::translation(10., 5., 7.);
/// let t = c * b * a;
/// assert_eq!(t * p, point(15., 0., 7.));
/// ```
#[derive(Copy, Clone, Default)]
pub struct Matrix4x4 {
m: [[Float; 4]; 4],
@ -529,6 +497,32 @@ mod tests {
tuples::{point, vector},
};
#[test]
fn example4x4() {
// Individual transformations are applied in sequence.
let p = point(1., 0., 1.);
let a = Matrix4x4::rotation_x(PI / 2.);
let b = Matrix4x4::scaling(5., 5., 5.);
let c = Matrix4x4::translation(10., 5., 7.);
// Apply rotation first.
let p2 = a * p;
assert_eq!(p2, point(1., -1., 0.));
// Then apply scaling.
let p3 = b * p2;
assert_eq!(p3, point(5., -5., 0.));
// Then apply translation.
let p4 = c * p3;
assert_eq!(p4, point(15., 0., 7.));
// Chained transformations must be applied in reverse order.
let p = point(1., 0., 1.);
let a = Matrix4x4::rotation_x(PI / 2.);
let b = Matrix4x4::scaling(5., 5., 5.);
let c = Matrix4x4::translation(10., 5., 7.);
let t = c * b * a;
assert_eq!(t * p, point(15., 0., 7.));
}
#[test]
fn translation() {
let transform = Matrix4x4::translation(5., -3., 2.);