fey_math/
ray.rs

1use crate::{Num, Vec2, impl_approx, impl_bytemuck, impl_casts};
2use serde::{Deserialize, Serialize};
3
4pub type RayF = Ray<f32>;
5
6/// A ray with an origin and direction.
7#[repr(C)]
8#[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Serialize, Deserialize)]
9pub struct Ray<T> {
10    pub origin: Vec2<T>,
11    pub direction: Vec2<T>,
12}
13
14impl_bytemuck!(Ray);
15
16impl_approx!(
17    NAME = Ray
18    FIELDS = (origin, direction)
19);
20
21impl_casts!(
22    NAME = Ray
23    FIELDS = (origin, direction)
24);
25
26/// Create a [`Ray`].
27#[inline]
28pub const fn ray<T>(origin: Vec2<T>, direction: Vec2<T>) -> Ray<T> {
29    Ray { origin, direction }
30}
31
32impl<T> Ray<T> {
33    /// Create a new ray.
34    #[inline]
35    pub const fn new(origin: Vec2<T>, direction: Vec2<T>) -> Self {
36        ray(origin, direction)
37    }
38}
39
40impl<T: Num> Ray<T> {
41    /// Get a point at the distance `dist` along the ray.
42    #[inline]
43    pub fn point(&self, dist: T) -> Vec2<T> {
44        self.origin + self.direction * dist
45    }
46}