From 9f92fe8775486e9482936dbb852cc678268902c5 Mon Sep 17 00:00:00 2001 From: Revar Desmera Date: Mon, 18 Nov 2019 21:11:38 -0800 Subject: [PATCH] Enhanced lerp() to allow list/range u values to return lists of results. --- math.scad | 5 +++-- tests/test_math.scad | 2 ++ version.scad | 2 +- 3 files changed, 6 insertions(+), 3 deletions(-) diff --git a/math.scad b/math.scad index 70dab70..2b97b30 100644 --- a/math.scad +++ b/math.scad @@ -319,9 +319,10 @@ function segs(r) = // Arguments: // a = First value. // b = Second value. -// u = The proportion from `a` to `b` to calculate. Valid range is 0.0 to 1.0, inclusive. +// u = The proportion from `a` to `b` to calculate. Valid range is 0.0 to 1.0, inclusive. If given as a list or range of values, returns a list of results. function lerp(a,b,u) = - (1-u)*a + u*b; + is_num(u)? (1-u)*a + u*b : + [for (v = u) lerp(a,b,v)]; // Function: hypot() diff --git a/tests/test_math.scad b/tests/test_math.scad index 994ae76..3ec78b7 100644 --- a/tests/test_math.scad +++ b/tests/test_math.scad @@ -219,6 +219,8 @@ module test_lerp() { assert(lerp(-20,20,0.5) == 0); assert(lerp(-20,20,0.75) == 10); assert(lerp(-20,20,1) == 20); + assert(lerp(-20,20,[0,0.25,0.5,0.75,1]) == [-20,-10,0,10,20]); + assert(lerp(-20,20,[0:0.25:1]) == [-20,-10,0,10,20]); assert(lerp([10,10],[30,-10],0.5) == [20,0]); } test_lerp(); diff --git a/version.scad b/version.scad index f418189..865fde8 100644 --- a/version.scad +++ b/version.scad @@ -8,7 +8,7 @@ ////////////////////////////////////////////////////////////////////// -BOSL_VERSION = [2,0,42]; +BOSL_VERSION = [2,0,43]; // Section: BOSL Library Version Functions