This commit is contained in:
Jon Janzen
2023-12-01 18:08:32 -07:00
parent e7f926ac1e
commit 6a5d95ae64
3 changed files with 25 additions and 3 deletions

View File

@@ -7,7 +7,9 @@ pub mod color;
pub mod canvas; pub mod canvas;
pub mod matrix; pub mod matrix;
pub mod transformations; pub mod transformations;
pub mod ray; pub mod ray;
pub mod sphere;
#[macro_export] #[macro_export]
macro_rules! num_traits_cast { macro_rules! num_traits_cast {

View File

@@ -4,13 +4,13 @@ use num_traits::NumCast;
use crate::structs::Tuple; use crate::structs::Tuple;
#[derive(Debug, PartialEq)] #[derive(Debug, PartialEq)]
struct Ray { pub struct Ray {
origin: Tuple, origin: Tuple,
direction: Tuple, direction: Tuple,
} }
impl Ray { impl Ray {
fn new(origin: Tuple, direction: Tuple) -> Option<Ray> { pub fn new(origin: Tuple, direction: Tuple) -> Option<Ray> {
if !origin.is_point() || !direction.is_vector() { if !origin.is_point() || !direction.is_vector() {
None None
} else { } else {
@@ -21,7 +21,7 @@ impl Ray {
} }
} }
fn position<X: NumCast>(&self, time: X) -> Tuple { pub fn position<X: NumCast>(&self, time: X) -> Tuple {
self.origin + self.direction * num_traits_cast!(time) self.origin + self.direction * num_traits_cast!(time)
} }
} }

20
features/src/sphere.rs Normal file
View File

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