From f792d1a6266f6783d1e673554f90e7a7ca82a50d Mon Sep 17 00:00:00 2001 From: Bill Thiede Date: Mon, 5 Jul 2021 17:49:12 -0700 Subject: [PATCH] matrices: implement Matrix4x4::scaling --- rtchallenge/src/matrices.rs | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/rtchallenge/src/matrices.rs b/rtchallenge/src/matrices.rs index f4cf7a4..294667a 100644 --- a/rtchallenge/src/matrices.rs +++ b/rtchallenge/src/matrices.rs @@ -249,6 +249,40 @@ impl Matrix4x4 { ) } + /// Creates a 4x4 matrix representing a scaling of x,y,z. + /// + /// # Examples + /// + /// ``` + /// use rtchallenge::{matrices::Matrix4x4, tuples::Tuple}; + /// + /// // A scaling matrix applied to a point. + /// let transform = Matrix4x4::scaling(2., 3., 4.); + /// let p = Tuple::point(-4., 6., 8.); + /// assert_eq!(transform * p, Tuple::point(-8., 18., 32.)); + /// + /// // A scaling matrix applied to a vector. + /// let v = Tuple::vector(-4., 6., 8.); + /// assert_eq!(transform * v, Tuple::vector(-8., 18., 32.)); + /// + /// // Multiplying by the inverse of a scaling matrix. + /// let inv = transform.inverse(); + /// assert_eq!(inv * v, Tuple::vector(-2., 2., 2.)); + /// + /// // Reflection is scaling by a negative value. + /// let transform = Matrix4x4::scaling(-1., 1., 1.); + /// let p = Tuple::point(2., 3., 4.); + /// assert_eq!(transform * p, Tuple::point(-2., 3., 4.)); + /// ``` + pub fn scaling(x: f32, y: f32, z: f32) -> Matrix4x4 { + Matrix4x4::new( + [x, 0., 0., 0.], + [0., y, 0., 0.], + [0., 0., z, 0.], + [0., 0., 0., 1.], + ) + } + /// Transpose self, returning a new matrix that has been reflected across the diagonal. /// # Examples ///