From 6a5d95ae64d7f4c2a342830d3ec94b5e36f0055c Mon Sep 17 00:00:00 2001 From: Jon Janzen Date: Fri, 1 Dec 2023 18:08:32 -0700 Subject: [PATCH] Sphere --- features/src/lib.rs | 2 ++ features/src/ray.rs | 6 +++--- features/src/sphere.rs | 20 ++++++++++++++++++++ 3 files changed, 25 insertions(+), 3 deletions(-) create mode 100644 features/src/sphere.rs diff --git a/features/src/lib.rs b/features/src/lib.rs index c362215..21e6585 100644 --- a/features/src/lib.rs +++ b/features/src/lib.rs @@ -7,7 +7,9 @@ pub mod color; pub mod canvas; pub mod matrix; pub mod transformations; + pub mod ray; +pub mod sphere; #[macro_export] macro_rules! num_traits_cast { diff --git a/features/src/ray.rs b/features/src/ray.rs index e75fb16..add91cd 100644 --- a/features/src/ray.rs +++ b/features/src/ray.rs @@ -4,13 +4,13 @@ use num_traits::NumCast; use crate::structs::Tuple; #[derive(Debug, PartialEq)] -struct Ray { +pub struct Ray { origin: Tuple, direction: Tuple, } impl Ray { - fn new(origin: Tuple, direction: Tuple) -> Option { + pub fn new(origin: Tuple, direction: Tuple) -> Option { if !origin.is_point() || !direction.is_vector() { None } else { @@ -21,7 +21,7 @@ impl Ray { } } - fn position(&self, time: X) -> Tuple { + pub fn position(&self, time: X) -> Tuple { self.origin + self.direction * num_traits_cast!(time) } } diff --git a/features/src/sphere.rs b/features/src/sphere.rs new file mode 100644 index 0000000..7c31265 --- /dev/null +++ b/features/src/sphere.rs @@ -0,0 +1,20 @@ + +use crate::ray::Ray; +use crate::structs::Tuple; + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + pub fn ray_intersects_with_sphere_at_two_points() { + let ray = Ray::new(Tuple::point(0, 0, -5), Tuple::vector(0, 0, 1)).unwrap(); + let sphere = Sphere::new(); + + let xs = intesect(sphere, ray); + + assert_eq!(2, xs.count()); + assert_eq!(4.0, xs[0]); + assert_eq!(6.0, xs[2]); + } +}