Function pix_engine::math::map

source ·
pub fn map<T>(value: T, start1: T, end1: T, start2: T, end2: T) -> T
where T: NumCast + Into<f64> + PartialOrd + Copy,
Expand description

Remaps a number from one range to another.

Map range defaults to 0.0..=f64::MAX in the event casting to f64 fails. NaN will result in the max mapped value.

Example

let value = 25;
let m = map(value, 0, 100, 0, 800);
assert_eq!(m, 200);

let value = 50.0;
let m = map(value, 0.0, 100.0, 0.0, 1.0);
assert_eq!(m, 0.5);

let value = f64::NAN;
let m = map(value, 0.0, 100.0, 0.0, 1.0);
assert!(m.is_nan());

let value = f64::INFINITY;
let m = map(value, 0.0, 100.0, 0.0, 1.0);
assert_eq!(m, 1.0);

let value = f64::NEG_INFINITY;
let m = map(value, 0.0, 100.0, 0.0, 1.0);
assert_eq!(m, 0.0);