Skip to main content

Interpolate

Trait Interpolate 

Source
pub trait Interpolate: Sized {
    // Required method
    fn lerp(&self, other: &Self, t: f32) -> Self;
}
Expand description

A value that supports linear interpolation between two instances.

Implement this trait for any type you want to animate with Animato. The library ships blanket impls for f32, f64, [f32; 2], [f32; 3], [f32; 4], i32, and u8.

§Contract

  • self.lerp(other, 0.0) must equal *self
  • self.lerp(other, 1.0) must equal *other
  • t outside [0.0, 1.0] is allowed — implementations may extrapolate or clamp

§Example

use animato_core::Interpolate;

let a = 0.0_f32;
let b = 100.0_f32;
assert_eq!(a.lerp(&b, 0.0), 0.0);
assert_eq!(a.lerp(&b, 1.0), 100.0);
assert_eq!(a.lerp(&b, 0.5), 50.0);

Required Methods§

Source

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

Linearly interpolate from self to other by factor t.

t = 0.0 returns self, t = 1.0 returns other.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementations on Foreign Types§

Source§

impl Interpolate for f32

Source§

fn lerp(&self, other: &f32, t: f32) -> f32

Direct float lerp — self + (other - self) * t.

Source§

impl Interpolate for f64

Source§

fn lerp(&self, other: &f64, t: f32) -> f64

Full-precision f64 lerp — t is cast to f64.

Source§

impl Interpolate for i32

Source§

fn lerp(&self, other: &i32, t: f32) -> i32

Lerps as f32 and rounds to the nearest integer.

Source§

impl Interpolate for u8

Source§

fn lerp(&self, other: &u8, t: f32) -> u8

Lerps as f32, rounds, and clamps to [0, 255].

Source§

impl Interpolate for [f32; 2]

Source§

fn lerp(&self, other: &[f32; 2], t: f32) -> [f32; 2]

Per-component lerp for 2D vectors.

Source§

impl Interpolate for [f32; 3]

Source§

fn lerp(&self, other: &[f32; 3], t: f32) -> [f32; 3]

Per-component lerp for 3D vectors.

Source§

impl Interpolate for [f32; 4]

Source§

fn lerp(&self, other: &[f32; 4], t: f32) -> [f32; 4]

Per-component lerp for 4D vectors (e.g. RGBA colors in linear space).

Implementors§