From 9befbd9ad2f2c6484766592fafb49a46d30d7356 Mon Sep 17 00:00:00 2001 From: Bill Thiede Date: Fri, 30 Jul 2021 21:59:40 -0700 Subject: [PATCH] matrices: moving another doctest to unit --- rtchallenge/src/matrices.rs | 58 +++++++++++++++++-------------------- 1 file changed, 26 insertions(+), 32 deletions(-) diff --git a/rtchallenge/src/matrices.rs b/rtchallenge/src/matrices.rs index b74ecc9..ba02aed 100644 --- a/rtchallenge/src/matrices.rs +++ b/rtchallenge/src/matrices.rs @@ -140,38 +140,6 @@ impl PartialEq for Matrix3x3 { /// Matrix4x4 represents a 4x4 matrix in row-major form. So, element `m[i][j]` corresponds to mi,j /// 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.);