Trait lerp::Lerp [] [src]

pub trait Lerp<F> {
    fn lerp(self, other: Self, t: F) -> Self;

    fn lerp_bounded(self, other: Self, t: F) -> Self where Self: Sized, F: PartialOrd + Copy + Zero + One { ... }
}

Types which are amenable to linear interpolation and extrapolation.

This is mainly intended to be useful for complex numbers, vectors, and other types which may be multiplied by a scalar while retaining their own type.

It's automatically implemented for all T: Copy + Add<Output = T> + Sub<Output = T> + Mul<F, Output = T>.

Required Methods

fn lerp(self, other: Self, t: F) -> Self

Interpolate and extrapolate between self and other using t as the parameter.

At t == 0.0, the result is equal to self. At t == 1.0, the result is equal to other. At all other points, the result is a mix of self and other, proportional to t.

t is unbounded, so extrapolation and negative interpolation are no problem.

Examples

Basic lerping on floating points:

use lerp::Lerp;

let four_32 = 3.0_f32.lerp(5.0, 0.5);
assert_eq!(four_32, 4.0);
let four_64 = 3.0_f64.lerp(5.0, 0.5);
assert_eq!(four_64, 4.0);

Extrapolation:

assert_eq!(3.0.lerp(4.0, 2.0), 5.0);

Negative extrapolation:

assert_eq!(3.0.lerp(4.0, -1.0), 2.0);

Reverse interpolation:

assert_eq!(5.0.lerp(3.0, 0.5), 4.0);

Provided Methods

fn lerp_bounded(self, other: Self, t: F) -> Self where Self: Sized, F: PartialOrd + Copy + Zero + One

Interpolate between self and other precisely per the lerp function, bounding t in the inclusive range [0..1].

Examples

Bounding on numbers greater than one:

assert_eq!(3.0.lerp_bounded(4.0, 2.0), 4.0);

Bounding on numbers less than zero:

assert_eq!(3.0.lerp_bounded(5.0, -2.0), 3.0);

Implementors