Compare commits
3 Commits
05c17b0179
...
cd297fd4a9
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
cd297fd4a9 | ||
|
|
910570bf37 | ||
|
|
f2056615be |
@@ -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
52
features/src/ray.rs
Normal 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);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user