sidewinder/ray.rs
1use crate::vec3::{Point, Vec3};
2
3/// **P**(*t*) = **A** + *t***b** where **P** is a position along a 3D line, **A** is the ray
4/// origin, and **b** is the ray direction. Change *t*, the distance from the origin, to affect the
5/// color seen along the ray.
6#[non_exhaustive]
7#[derive(Debug, Default, Clone, Copy, PartialEq, PartialOrd)]
8pub struct Ray {
9 pub origin: Point,
10 pub direction: Vec3,
11}
12
13impl Ray {
14 #[inline]
15 #[must_use]
16 pub const fn new(origin: Point, direction: Vec3) -> Self {
17 Self { origin, direction }
18 }
19
20 /// Get a location along a ray path using the distance `t` from the ray origin.
21 #[inline]
22 #[must_use]
23 pub fn at(self, t: f64) -> Point {
24 self.direction.mul_add(t, self.origin) // self.origin + t * self.direction
25 }
26}