common/
ray.rs

1use crate::vec3::{Point3, Vec3};
2
3/// A ray defined by an origin and a direction.
4pub struct Ray {
5    /// Ray origin.
6    pub orig: Point3,
7    /// Ray direction.
8    pub dir: Vec3,
9}
10
11impl Ray {
12    /// Creates a new ray from `origin` and `direction`.
13    pub fn new(origin: Point3, direction: Vec3) -> Self {
14        Ray { orig: origin, dir: direction }
15    }
16
17    /// Returns the ray origin.
18    pub fn origin(&self) -> Point3 { self.orig }
19    /// Returns the ray direction.
20    pub fn direction(&self) -> Vec3 { self.dir }
21
22    /// Returns the point at parameter `t` along the ray: **P**(*t*) = **orig** + *t* **dir**.
23    pub fn at(&self, t: f64) -> Point3 {
24        self.orig + t * self.dir
25    }
26}