From 2c1b5354fa68f4b3d3f1a7205e6ecaaa1a8f04c9 Mon Sep 17 00:00:00 2001 From: Jon Janzen Date: Fri, 3 Sep 2021 20:17:18 -0600 Subject: [PATCH] rotation complete --- features/src/transformations.rs | 38 +++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/features/src/transformations.rs b/features/src/transformations.rs index ce43d6e..9fb1bef 100644 --- a/features/src/transformations.rs +++ b/features/src/transformations.rs @@ -30,6 +30,24 @@ impl Matrix { [0.0, 0.0, 0.0, 1.0], ]) } + + pub fn rotation_y(r: f32) -> Self { + Matrix::from_array([ + [r.cos(), 0.0, r.sin(), 0.0], + [0.0, 1.0, 0.0, 0.0], + [-1.0 * r.sin(), 0.0, r.cos() , 0.0], + [0.0, 0.0, 0.0, 1.0], + ]) + } + + pub fn rotation_z(r: f32) -> Self { + Matrix::from_array([ + [r.cos(), -1.0 * r.sin(), 0.0, 0.0], + [r.sin(), r.cos(), 0.0, 0.0], + [0.0, 0.0, 1.0, 0.0], + [0.0, 0.0, 0.0, 1.0], + ]) + } } #[cfg(test)] @@ -119,4 +137,24 @@ mod tests { assert_eq!(&inv * &p, Tuple::point(0.0, sqrt_of_2() / 2.0, -1.0 * sqrt_of_2() / 2.0)); } + + #[test] + fn rotating_point_around_y_axis() { + let p = Tuple::point(0.0, 0.0, 1.0); + let half_quarter = Matrix::rotation_y(PI / 4.0); + let full_quarter = Matrix::rotation_y(PI / 2.0); + + assert_eq!(&half_quarter * &p, Tuple::point(sqrt_of_2() / 2.0, 0.0, sqrt_of_2() / 2.0)); + assert_eq!(&full_quarter * &p, Tuple::point(1.0, 0.0, 0.0)); + } + + #[test] + fn rotating_point_around_z_axis() { + let p = Tuple::point(0.0, 1.0, 0.0); + let half_quarter = Matrix::rotation_z(PI / 4.0); + let full_quarter = Matrix::rotation_z(PI / 2.0); + + assert_eq!(&half_quarter * &p, Tuple::point(-1.0 * sqrt_of_2() / 2.0, sqrt_of_2() / 2.0, 0.0)); + assert_eq!(&full_quarter * &p, Tuple::point(-1.0, 0.0, 0.0)); + } }