Compare commits
2 Commits
cd297fd4a9
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
6a5d95ae64 | ||
|
|
e7f926ac1e |
@@ -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 {
|
||||
|
||||
@@ -1,13 +1,16 @@
|
||||
use crate::num_traits_cast;
|
||||
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<Ray> {
|
||||
pub fn new(origin: Tuple, direction: Tuple) -> Option<Ray> {
|
||||
if !origin.is_point() || !direction.is_vector() {
|
||||
None
|
||||
} else {
|
||||
@@ -17,6 +20,10 @@ impl Ray {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
pub fn position<X: NumCast>(&self, time: X) -> Tuple {
|
||||
self.origin + self.direction * num_traits_cast!(time)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -49,4 +56,15 @@ mod tests {
|
||||
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