Recalculate a lerp percentage value, so that it returns the same value, even when the max lerp value is adjusted

interpolation

I currently have a lerp function, which is
$$
y = p \cdot x_2 + (1 – p) \cdot x_1,
$$

where

  • $x_1$ is the min lerp value,
  • $x_2$ is the max lerp value,
  • $p$ is the percentage to lerp between $x_1$ and $x_2$, in a $0.0$ to $1.0$ format, and
  • $y$ is the result of the lerp function.

I was wondering if, by adjusting $x_2$, do there exists a specific formula I could use to recalculate $p$, so that it would output the same result, even with the new $x_2$ value? Is there also a formula I can use for if $x_1$ is adjusted?

I'm sorry if the question is not very easy to understand, if you would like me to clarify anything, please just ask me.

Thank you for your time!

Best Answer

Let $f(a,b,t)=tb+a(1-t)$. Then, we are looking for a value $t_2$ such that $f(a,b_1,t_1)=f(a,b_2,t_2)$ where $a$ represents the source value, $b_1$ represents the destination value, $t_1$ represents the interpolation factor, and $b_2$ and $t_2$ represent the new source and interplation factor respectively. If we substitute and rearrange: $$ \begin{align*} f(a,b_1,t_1)&=f(a,b_2,t_2);\\ t_1b_1+a(1-t_1)&=t_2b_2+a(1-t_2);\\ t_1(b_1-a)&=t_2(b_2-a);\\ t_2&=\boxed{\frac{t_1(b_1-a)}{b_2-a}}, \end{align*} $$ which is the formula for the interpolation percentage such that the interpolated value stays the same.

Related Question