[][src]Trait lerp::Lerp

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: Add<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);
Loading content...

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);
Loading content...

Implementors

impl<T, F> Lerp<F> for T where
    T: Add<Output = T> + Mul<F, Output = T>,
    F: Float
[src]

Default, generic implementation of Lerp.

Note that due to the implementation details, LerpIterator is only actually an iterator for those types T which fit the constraint Mul<f64, Output = T>. This means that though you can use the lerp method on f32s, it will not work to iterate over the results of calling lerp_iter on an f32. Instead, up-cast your f32 as an f64 before calling: (example_f32 as f64).lerp_iter(...).

This default implementation 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.

Loading content...