-
Need help calculating “linear()” function in JS Math
Hi everyone,
I’m trying to port some of my AE methods to another program that’s also using Javascript control (Quartz Composer), and need to determine a strictly mathematical equivalent to AE’s “linear()” interpolation method.
I realize this is therefore not an AE question, but it began with AE so please bear with me 😉
I found another post here, asking nearly the same question and the answer to “what is the long-form version of the linear() function” was this:
result = value1 + ((t - t_min)/(t_max - t_min))*(value2 - value1);…but in this answer (by Dan Ebberts) it was noted that AE’s linear() function clamps the value, so you’d also have to add a clamp to it. i.e.:
clampedresult = clamp(result, value1, value2);however, in porting to Quartz Composer, I can’t use this either because “clamp()” doesn’t exist in Javascript outside of AE, or at least it doesn’t in QC, where we basically just have javascript math. I looked up Javascript approaches to defining a clamp function and they are all of the following form:
Math.min(Math.max(result, value1), value2);but the problem with that is that it cannot handle descending values. AE’s linear() function doesn’t care whether value1 is less or greater than value2. It will simply interpolate between any two numbers, as t1 goes to t2. In fact you can even reverse the t numbers and the interpolation will go in reverse. So I need a more robust mathematical definition here, something that can handle both ascending and descending interpolations. If it helps, I am only using it in the case where the reference number (t) goes from 0 to 1, so the code could be simpler there; but value1 and value2 could be anything.
Really hoping someone can crack this one for me….
Lu Nelson
Berlin, Germany
—
[MacProQ2.66, 8GB, Sys10.5.6, FCP 6.0.5, AE 9.0.2]