Compare commits

...

3 Commits

Author SHA1 Message Date
Jon Janzen
cd297fd4a9 enforce point and vector for origin and direction 2022-03-11 18:35:11 -07:00
Jon Janzen
910570bf37 Can create ray 2022-03-11 18:23:26 -07:00
Jon Janzen
f2056615be added ray.rs 2022-03-11 18:15:39 -07:00
2 changed files with 53 additions and 0 deletions

View File

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

52
features/src/ray.rs Normal file
View File

@@ -0,0 +1,52 @@
use crate::structs::Tuple;
#[derive(Debug, PartialEq)]
struct Ray {
origin: Tuple,
direction: Tuple,
}
impl Ray {
fn new(origin: Tuple, direction: Tuple) -> Option<Ray> {
if !origin.is_point() || !direction.is_vector() {
None
} else {
Some(Ray {
origin,
direction,
})
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
pub fn create_a_ray() {
let origin = Tuple::point(1, 2, 3);
let direction = Tuple::vector(4, 5, 6);
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::<i32, i32, i32>(4, 5, 6);
let ray = Ray::new(direction, direction);
assert_eq!(ray, None);
}
#[test]
pub fn direction_must_be_vector() {
let point = Tuple::point::<i32, i32, i32>(4, 5, 6);
let ray = Ray::new(point, point);
assert_eq!(ray, None);
}
}