-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8cdd343
commit 1af881f
Showing
3 changed files
with
38 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
use crate::{Intersect, Ray}; | ||
use nalgebra::Point3; | ||
|
||
pub struct Sphere { | ||
center: Point3<f64>, | ||
radius: f64, | ||
} | ||
|
||
impl Sphere { | ||
pub fn new(center: Point3<f64>, radius: f64) -> Self { | ||
Self { center, radius } | ||
} | ||
} | ||
|
||
impl Intersect for Sphere { | ||
fn intersect(&self, ray: &Ray) -> Option<f64> { | ||
let a = ray.dir.magnitude_squared(); | ||
let b = ray.dir.dot(&(self.center - ray.orig)); | ||
let c = (self.center - ray.orig).magnitude_squared() - self.radius * self.radius; | ||
let discriminant = b * b - a * c; | ||
if discriminant < 0.0 { | ||
return None; | ||
} | ||
Some((b - discriminant.sqrt()) / a) | ||
} | ||
} |