From: Alexis Laferrière Date: Wed, 4 Mar 2015 02:01:52 +0000 (-0500) Subject: lib: intro `Float.lerp` for simple linear interpolation X-Git-Tag: v0.7.3~32^2~3 X-Git-Url: http://nitlanguage.org lib: intro `Float.lerp` for simple linear interpolation Signed-off-by: Alexis Laferrière --- diff --git a/lib/standard/math.nit b/lib/standard/math.nit index 14c0e40..eec0cf6 100644 --- a/lib/standard/math.nit +++ b/lib/standard/math.nit @@ -195,7 +195,7 @@ redef class Float # assert -1.34.round == -1.0 # assert -1.67.round == -2.0 fun round: Float is extern "round" - + # Returns a random `Float` in `[0.0 .. self[`. fun rand: Float is extern "kernel_Float_Float_rand_0" @@ -220,6 +220,16 @@ redef class Float end private fun is_inf_extern: Bool is extern "isinf" + + # Linear interpolation between `a` and `b` using `self` as weight + # + # ~~~ + # assert 0.0.lerp(0.0, 128.0) == 0.0 + # assert 0.5.lerp(0.0, 128.0) == 64.0 + # assert 1.0.lerp(0.0, 128.0) == 128.0 + # assert -0.5.lerp(0.0, 128.0) == -64.0 + # ~~~ + fun lerp(a, b: Float): Float do return (1.0 - self) * a + self * b end redef class Collection[ E ]