Translate and scale a ray
This commit is contained in:
parent
8e4f6b06e6
commit
af7d8680a0
|
@ -1,7 +1,6 @@
|
||||||
|
use crate::types::{Matrix, Point, Sphere, Vector};
|
||||||
use std::cmp::Ordering;
|
use std::cmp::Ordering;
|
||||||
|
|
||||||
use crate::types::{Point, Sphere, Vector};
|
|
||||||
|
|
||||||
#[derive(Clone, Debug, PartialEq)]
|
#[derive(Clone, Debug, PartialEq)]
|
||||||
pub struct Intersection<'a> {
|
pub struct Intersection<'a> {
|
||||||
t: f64,
|
t: f64,
|
||||||
|
@ -65,13 +64,22 @@ impl Ray {
|
||||||
vec![
|
vec![
|
||||||
Intersection { t: t1, object: &s },
|
Intersection { t: t1, object: &s },
|
||||||
Intersection { t: t2, object: &s },
|
Intersection { t: t2, object: &s },
|
||||||
].into()
|
]
|
||||||
|
.into()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn transform(&self, m: Matrix) -> Self {
|
||||||
|
Self {
|
||||||
|
origin: m.clone() * self.origin,
|
||||||
|
direction: m * self.direction,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
use crate::transforms::{scaling, translation};
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn computing_point_from_distance() {
|
fn computing_point_from_distance() {
|
||||||
|
@ -173,4 +181,22 @@ mod tests {
|
||||||
|
|
||||||
assert_eq!(xs.hit(), Some(&i4));
|
assert_eq!(xs.hit(), Some(&i4));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn translate_a_ray() {
|
||||||
|
let r = Ray::new(Point::new(1., 2., 3.), Vector::new(0., 1., 0.));
|
||||||
|
let m = translation(3., 4., 5.);
|
||||||
|
let r2 = r.transform(m);
|
||||||
|
assert_eq!(r2.origin, Point::new(4., 6., 8.));
|
||||||
|
assert_eq!(r2.direction, Vector::new(0., 1., 0.));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn scale_a_ray() {
|
||||||
|
let r = Ray::new(Point::new(1., 2., 3.), Vector::new(0., 1., 0.));
|
||||||
|
let m = scaling(2., 3., 4.);
|
||||||
|
let r2 = r.transform(m);
|
||||||
|
assert_eq!(r2.origin, Point::new(2., 6., 12.));
|
||||||
|
assert_eq!(r2.direction, Vector::new(0., 3., 0.,));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue