From 3952a8ba83f063f103ef95c58edc335e48e6cca0 Mon Sep 17 00:00:00 2001 From: Bill Thiede Date: Thu, 24 Jun 2021 11:10:11 -0700 Subject: [PATCH] Implement point/vector constructors. --- rtchallenge/src/tuples.rs | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/rtchallenge/src/tuples.rs b/rtchallenge/src/tuples.rs index 58f2540..75f2fe2 100644 --- a/rtchallenge/src/tuples.rs +++ b/rtchallenge/src/tuples.rs @@ -1,3 +1,4 @@ +#[derive(Debug, PartialEq)] struct Tuple { x: f32, y: f32, @@ -15,15 +16,23 @@ impl Tuple { } } +fn point(x: f32, y: f32, z: f32) -> Tuple { + tuple(x, y, z, 1.0) +} + +fn vector(x: f32, y: f32, z: f32) -> Tuple { + tuple(x, y, z, 0.0) +} + fn tuple(x: f32, y: f32, z: f32, w: f32) -> Tuple { Tuple { x, y, z, w } } #[cfg(test)] mod tests { - use super::tuple; + use super::{point, tuple, vector}; #[test] - fn point() { + fn is_point() { // A tuple with w = 1 is a point let a = tuple(4.3, -4.2, 3.1, 1.0); assert_eq!(a.x, 4.3); @@ -34,7 +43,7 @@ mod tests { assert!(!a.is_vector()); } #[test] - fn vector() { + fn is_vector() { // A tuple with w = 0 is a point let a = tuple(4.3, -4.2, 3.1, 0.0); assert_eq!(a.x, 4.3); @@ -44,4 +53,12 @@ mod tests { assert!(!a.is_point()); assert!(a.is_vector()); } + #[test] + fn point_tuple() { + assert_eq!(point(4., -4., 3.), tuple(4., -4., 3., 1.)) + } + #[test] + fn vector_tuple() { + assert_eq!(vector(4., -4., 3.), tuple(4., -4., 3., 0.)) + } }