From 6a7a3c777490ab253a639f5b523551816a5255e9 Mon Sep 17 00:00:00 2001 From: bijan2005 Date: Mon, 30 Nov 2020 09:00:35 -0500 Subject: [PATCH] Plane-ray intersection test --- README.md | 4 ++-- src/object.rs | 1 + src/object/plane.rs | 9 ++++++++- 3 files changed, 11 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index f5777a3..c019091 100644 --- a/README.md +++ b/README.md @@ -15,8 +15,8 @@ This list may be changed or extended in the future. - [x] Sphere normal generation - [x] Color mapping on spheres - [ ] Plane objects - - [ ] Plane struct - - [ ] Plane intersection test + - [x] Plane struct + - [x] Plane intersection test - [ ] Color mapping on planes - [ ] Triangle objects - [ ] Triangle struct diff --git a/src/object.rs b/src/object.rs index cbd6b0e..9be2ca6 100644 --- a/src/object.rs +++ b/src/object.rs @@ -1,5 +1,6 @@ mod sphere; pub use sphere::*; +mod plane; pub use plane::*; mod triangle; pub use triangle::*; use na::*; diff --git a/src/object/plane.rs b/src/object/plane.rs index 59ffae2..8e50e51 100644 --- a/src/object/plane.rs +++ b/src/object/plane.rs @@ -27,7 +27,14 @@ impl Plane { { Plane::new(center, normal, move |_, _| color) } pub fn intersect(&self, ray: Ray) -> Option { - unimplemented!() + + let d = self.normal.dot(&ray.direction); + if d < 1e-6 { return None; } + + let t = (self.center - ray.origin).dot(&*self.normal) / d; + + if t >= 0.0 { Some(t) } + else { None } } pub fn getcolor(&self, point: Point3) -> Color {