pub trait Lerp {
// Required method
fn lerp(&self, y1: &Self, x: f32) -> Self;
}Expand description
Trait for a type that supports the standard lerp (linear interpolation) operation.
Linear interpolation refers mathematically to computing the y value of the straight line
connecting two points (x0, y0) and (x1, y1) at a given x position. This requires solving
for the equality: (y - y0) / (x - x0) = (y1 - y0) / (x1 - x0). Lerp assumes a normalized x
value, such that x0 = 0 and x1 = 1, which reduces the equation to:
lerp(y0, y1, x) = y0 + x(y1 - y0)
All primitive numeric types are implicitly lerpable, although the computation is performed
using 32-bit floating-point arithmetic, so there may be some precision loss when interpolating
with a type that is narrower (e.g. u32) or wider (f64). For any other type that is
composed entirely of numeric values, the trait can be implemented by lerping all of the
individual values.
Required Methods§
sourcefn lerp(&self, y1: &Self, x: f32) -> Self
fn lerp(&self, y1: &Self, x: f32) -> Self
Computes the linear interpolation between this value (y0) and a second (y1) value of the
same type, at normalized (from 0 to 1) position x.
Panics
The default implementation for primitives will panic if self or y1 are too large to fit
in an f32, or if the resulting interpolated value is out of bounds for the y type.
Example
use mina_core::interpolation::Lerp;
let y0: f32 = 5.0;
let y1: f32 = 15.0;
assert_eq!(y0.lerp(&y1, 0.0), 5.0);
assert_eq!(y0.lerp(&y1, 0.25), 7.5);
assert_eq!(y0.lerp(&y1, 0.5), 10.0);
assert_eq!(y0.lerp(&y1, 1.0), 15.0);