rotation complete

This commit is contained in:
Jon Janzen
2021-09-03 20:17:18 -06:00
parent 5fb429b9d0
commit 2c1b5354fa

View File

@@ -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));
}
}