I've seen a couple of lerp functions, mainly for lerping vectors, and they seem to go like this:
vector lerp(float bias, vector start, vector end)
{ return (1-bias) * start + bias * end; }
Whereas my naive way of doing it would be:
vector lerp(float bias, vector start, vector end)
{ return (end - start) * bias + start;
I following shows the breakdown of the two methods:
two float by vector multiplications | one vector addition | one float subtraction |
one float by vector multiplication | one vector addition and one vector subtraction |
Simplified, that means:
6 float multiplications | 3 additions | one float subtraction
3 float multiplications | 6 additions |
Am I mixed and have got the wrong idea that these are equivalent? I struggle sometimes with simple math concepts.
Edit: I just realised, in my case I need a halfway lerp, which is even cheaper to be done by getting the average of the two vectors' components. That's simply one addition and one multiplication for each axis, X, Y, Z. I think I'll do that.
This is a tradition that comes from Mathematics and has to do with affine transformations, a sort of transformations that map vectors in vectors in a linear way without the restriction of having to map the origin onto itself.
Linear transformations satisfy
f(a1*v1 + ... + an*vn) = a1*f(v1) + ... + an*f(vn)
Affine transformations satisfy the same provided that
a1 + a2 + ... + an = 1.
Why? Because an affine transformation happens to be of the form f() + c
, for some linear transformation f()
and some constant c
.
An affine combination is an expression of the form
a1*v1 + ... + an*vn
where the sum of the ai
is 1
. They are a special case of liner combinations.
Now, if you have only two points A
and B
in whatever dimension (1, 2, 3, etc.) the straight line defined by A
to B
can be seen as the place where all affine combinations live:
s*A + t*B (s + t = 1)
In this special case of only two points, you can also express them with only one parameter
(1 - t)*A + t*B.
When t = 0
your are in point A
, when t=0.5
you are right in the middle between A
and B
and when t=1
, you are in B
.
So, you can think of t
as the time, and consider that you are traveling from A
to B
when t
goes from 0
to 1
. Negative values of the parameter t
correspond to points on the line but not in the segment, and the same holds for t > 1
.
In other words, it is totally ok to use (B - A)*t + A
(which again, is valid in any dimension) except that (1-t)*A + t*B
makes it apparent the link with affine geometry.