Skip to main content

game_gem/math/
lerp.rs

1//! Generic linear interpolation trait.
2
3/// Types that can be linearly interpolated.
4pub trait Lerp: Sized {
5    /// Linear interpolation from `self` toward `other` by factor `t` (clamped 0..1).
6    fn lerp(self, other: Self, t: f32) -> Self;
7}
8
9impl Lerp for f32 {
10    #[inline]
11    fn lerp(self, other: Self, t: f32) -> Self {
12        self + (other - self) * t.clamp(0.0, 1.0)
13    }
14}
15
16impl Lerp for super::Vec2 {
17    #[inline]
18    fn lerp(self, other: Self, t: f32) -> Self {
19        self + (other - self) * t.clamp(0.0, 1.0)
20    }
21}
22
23impl Lerp for super::Vec3 {
24    #[inline]
25    fn lerp(self, other: Self, t: f32) -> Self {
26        self + (other - self) * t.clamp(0.0, 1.0)
27    }
28}
29
30impl Lerp for super::Vec4 {
31    #[inline]
32    fn lerp(self, other: Self, t: f32) -> Self {
33        self + (other - self) * t.clamp(0.0, 1.0)
34    }
35}