From bbdad2d96ae5da4ad07ff18de87b8b596ffb9218 Mon Sep 17 00:00:00 2001 From: Jean Privat Date: Mon, 21 Sep 2015 15:22:14 -0400 Subject: [PATCH] =?utf8?q?core/math:=20add=20basic=20B=C3=A9zier=20interpola?= =?utf8?q?tions=20on=20Float?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jean Privat --- lib/core/math.nit | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/lib/core/math.nit b/lib/core/math.nit index 50b36e9..c08c735 100644 --- a/lib/core/math.nit +++ b/lib/core/math.nit @@ -333,6 +333,45 @@ redef class Float # assert -0.5.lerp(0.0, 128.0) == -64.0 # ~~~ fun lerp(a, b: Float): Float do return (1.0 - self) * a + self * b + + # Quadratic Bézier interpolation between `a` and `b` with an `handle` using `self` as weight + # + # ~~~ + # assert 0.00.qerp(0.0, 32.0, 128.0) == 0.0 + # assert 0.25.qerp(0.0, 32.0, 128.0) == 20.0 + # assert 0.50.qerp(0.0, 32.0, 128.0) == 48.0 + # assert 0.75.qerp(0.0, 32.0, 128.0) == 84.0 + # assert 1.00.qerp(0.0, 32.0, 128.0) == 128.0 + # ~~~ + fun qerp(a, handle, b: Float): Float do + var p = self + var i = 1.0 - p + var r = i*i * a + + 2.0*i*p * handle + + p*p * b + return r + end + + # Cubic Bézier interpolation between `a` and `b` with two handles using `self` as weight + # + # The Cubic Bézier interpolation is the most common one and use two control points. + # + # ~~~ + # assert 0.00.cerp(0.0, 32.0, 128.0, 64.0) == 0.0 + # assert 0.25.cerp(0.0, 32.0, 128.0, 64.0) == 32.5 + # assert 0.50.cerp(0.0, 32.0, 128.0, 64.0) == 68.0 + # assert 0.75.cerp(0.0, 32.0, 128.0, 64.0) == 85.5 + # assert 1.00.cerp(0.0, 32.0, 128.0, 64.0) == 64.0 + # ~~~ + fun cerp(a, a_handle, b_handle, b: Float): Float do + var p = self + var i = 1.0 - p + var r = i*i*i * a + + 3.0*i*i*p * a_handle + + 3.0*i*p*p * b_handle + + p*p*p * b + return r + end end # Positive float infinite (IEEE 754) -- 1.7.9.5