emath

Function inverse_lerp

Source
pub fn inverse_lerp<R>(range: RangeInclusive<R>, value: R) -> Option<R>
where R: Copy + PartialEq + Sub<R, Output = R> + Div<R, Output = R>,
Expand description

Where in the range is this value? Returns 0-1 if within the range.

Returns <0 if before and >1 if after.

Returns None if the input range is zero-width.

assert_eq!(inverse_lerp(1.0..=5.0, 1.0), Some(0.0));
assert_eq!(inverse_lerp(1.0..=5.0, 3.0), Some(0.5));
assert_eq!(inverse_lerp(1.0..=5.0, 5.0), Some(1.0));
assert_eq!(inverse_lerp(1.0..=5.0, 9.0), Some(2.0));
assert_eq!(inverse_lerp(1.0..=1.0, 3.0), None);