Compare commits
5 Commits
05c17b0179
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
6a5d95ae64 | ||
|
|
e7f926ac1e | ||
|
|
cd297fd4a9 | ||
|
|
910570bf37 | ||
|
|
f2056615be |
@@ -8,6 +8,9 @@ pub mod canvas;
|
||||
pub mod matrix;
|
||||
pub mod transformations;
|
||||
|
||||
pub mod ray;
|
||||
pub mod sphere;
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! num_traits_cast {
|
||||
($tt:expr) => {
|
||||
|
||||
70
features/src/ray.rs
Normal file
70
features/src/ray.rs
Normal file
@@ -0,0 +1,70 @@
|
||||
use crate::num_traits_cast;
|
||||
use num_traits::NumCast;
|
||||
|
||||
use crate::structs::Tuple;
|
||||
|
||||
#[derive(Debug, PartialEq)]
|
||||
pub struct Ray {
|
||||
origin: Tuple,
|
||||
direction: Tuple,
|
||||
}
|
||||
|
||||
impl Ray {
|
||||
pub fn new(origin: Tuple, direction: Tuple) -> Option<Ray> {
|
||||
if !origin.is_point() || !direction.is_vector() {
|
||||
None
|
||||
} else {
|
||||
Some(Ray {
|
||||
origin,
|
||||
direction,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
pub fn position<X: NumCast>(&self, time: X) -> Tuple {
|
||||
self.origin + self.direction * num_traits_cast!(time)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#[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);
|
||||
}
|
||||
|
||||
#[test]
|
||||
pub fn create_and_query_ray() {
|
||||
let origin = Tuple::point(2, 3, 4);
|
||||
let direction = Tuple::vector(1, 0, 0);
|
||||
|
||||
let ray = Ray::new(origin, direction).unwrap();
|
||||
assert_eq!(Tuple::point(2, 3, 4), ray.position(0));
|
||||
assert_eq!(Tuple::point(3, 3, 4), ray.position(1));
|
||||
assert_eq!(Tuple::point(1, 3, 4), ray.position(-1));
|
||||
assert_eq!(Tuple::point(4.5, 3, 4), ray.position(2.5));
|
||||
}
|
||||
}
|
||||
20
features/src/sphere.rs
Normal file
20
features/src/sphere.rs
Normal 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]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user