From cd297fd4a956a871bd72dcc885cd72309043b3e7 Mon Sep 17 00:00:00 2001 From: Jon Janzen Date: Fri, 11 Mar 2022 18:35:11 -0700 Subject: [PATCH] enforce point and vector for origin and direction --- features/src/ray.rs | 31 ++++++++++++++++++++++++++----- 1 file changed, 26 insertions(+), 5 deletions(-) diff --git a/features/src/ray.rs b/features/src/ray.rs index 0491fea..87aa2ef 100644 --- a/features/src/ray.rs +++ b/features/src/ray.rs @@ -1,15 +1,20 @@ use crate::structs::Tuple; +#[derive(Debug, PartialEq)] struct Ray { origin: Tuple, direction: Tuple, } impl Ray { - fn new(origin: Tuple, direction: Tuple) -> Ray { - Ray { - origin, - direction, + fn new(origin: Tuple, direction: Tuple) -> Option { + if !origin.is_point() || !direction.is_vector() { + None + } else { + Some(Ray { + origin, + direction, + }) } } } @@ -24,8 +29,24 @@ mod tests { let origin = Tuple::point(1, 2, 3); let direction = Tuple::vector(4, 5, 6); - let ray = Ray::new(origin, direction); + let ray = Ray::new(origin, direction).unwrap(); assert_eq!(ray.origin, origin); assert_eq!(ray.direction, direction); } + + #[test] + pub fn origin_must_be_point() { + let direction = Tuple::vector::(4, 5, 6); + + let ray = Ray::new(direction, direction); + assert_eq!(ray, None); + } + + #[test] + pub fn direction_must_be_vector() { + let point = Tuple::point::(4, 5, 6); + let ray = Ray::new(point, point); + assert_eq!(ray, None); + } + }