1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
use number_traits::{Cast, Num}; #[inline] pub fn linear<T, F>(p0: T, p1: T, t: F) -> T where T: Copy + Num + Cast<F>, F: Copy + Num + Cast<T>, { if t <= F::zero() { p0 } else if t >= F::one() { p1 } else { let x0 = p0.cast(); let x1 = p1.cast(); (x0 + (x1 - x0) * t).cast() } } #[test] fn test_linear() { assert_eq!(linear(0, 200, 0.25), 50); assert_eq!(linear(0, 200, 0.5), 100); assert_eq!(linear(0, 200, 0.75), 150); }