spheres: implement rtiow Sphere::intersect for comparison.
This commit is contained in:
parent
19b2ef6ded
commit
37048ddd52
@ -193,6 +193,10 @@ impl Sphere {
|
||||
/// assert_eq!(xs.len(), 0);
|
||||
/// ```
|
||||
pub fn intersect<'s>(sphere: &'s Sphere, ray: &Ray) -> Intersections<'s> {
|
||||
intersect_rtc(sphere, ray)
|
||||
}
|
||||
|
||||
fn intersect_rtc<'s>(sphere: &'s Sphere, ray: &Ray) -> Intersections<'s> {
|
||||
let ray = ray.transform(sphere.inverse_transform);
|
||||
let sphere_to_ray = ray.origin - Tuple::point(0., 0., 0.);
|
||||
let a = dot(ray.direction, ray.direction);
|
||||
@ -200,11 +204,25 @@ pub fn intersect<'s>(sphere: &'s Sphere, ray: &Ray) -> Intersections<'s> {
|
||||
let c = dot(sphere_to_ray, sphere_to_ray) - 1.;
|
||||
let discriminant = b * b - 4. * a * c;
|
||||
if discriminant < 0. {
|
||||
Intersections::default()
|
||||
} else {
|
||||
Intersections::new(vec![
|
||||
Intersection::new((-b - discriminant.sqrt()) / (2. * a), &sphere),
|
||||
Intersection::new((-b + discriminant.sqrt()) / (2. * a), &sphere),
|
||||
])
|
||||
return Intersections::default();
|
||||
}
|
||||
Intersections::new(vec![
|
||||
Intersection::new((-b - discriminant.sqrt()) / (2. * a), &sphere),
|
||||
Intersection::new((-b + discriminant.sqrt()) / (2. * a), &sphere),
|
||||
])
|
||||
}
|
||||
fn intersect_rtiow<'s>(sphere: &'s Sphere, ray: &Ray) -> Intersections<'s> {
|
||||
let ray = ray.transform(sphere.inverse_transform);
|
||||
let oc = ray.origin - Tuple::point(0., 0., 0.);
|
||||
let a = dot(ray.direction, ray.direction);
|
||||
let b = dot(oc, ray.direction);
|
||||
let c = dot(oc, oc) - 1.;
|
||||
let discriminant = b * b - a * c;
|
||||
if discriminant < 0. {
|
||||
return Intersections::default();
|
||||
}
|
||||
Intersections::new(vec![
|
||||
Intersection::new((-b - discriminant.sqrt()) / a, &sphere),
|
||||
Intersection::new((-b + discriminant.sqrt()) / a, &sphere),
|
||||
])
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user