I'm looking for a non-linear way of interpolating between 4 values within a games engine.
I have a unit square abcd
. It has a different value for each edge ABCD
. Within that square there is some point x
. I also have exact position of x
within that square in 0..1 range by X and Y axis.
0 --> X
|
V aAAAAAAAb
Y D B
D x B
D B
dCCCCCCCc
Now I need to interpolate between values of ABCD
edges in a way, that near the edge the value of x
matches the edges value, but farther from the edge it gets mixed with other edges proportionally to the distances.
For example:
// Trivial case - mix proportionally
X = 0.1
Y = 0.1
x = A * 0.45 + B * 0.05 + C * 0.05 + D * 0.45; // Sum = 1
// Near edge case - B and D get penalized
X = 0.1
Y = 0.05
x = A * 0.9 + B * 0.025 + C * 0.05 + D * 0.45; // Sum should be = 1, how?
// Edge case
X = 0.1
Y = 0
x = A * 1.0 + B * 0 + C * 0 + D * 0; // Sum = 1
// Degenerate case
X = 0
Y = 0
x = A * 0.5 + B * 0 + C * 0 + D * 0.5; // Sum = 1
What kind of interpolation formula I could use to get the continuous desired result? Preferably fast in computation (to be used in games render loop).
P.S. This is my first post on Math, so please comment if there are any issues with it.
Best Answer
I have come up with a simpler equation while formulating my question.
Idea is to interpolate between X and Y axis proportionally to distance to them.
Let me guide you through with an example:
Sorry I'm not familiar with Excel formulas close enough to build a diagram, but it looks much alike this:
Benefits are: no need for iterations, knowing
ABCD
values andX
Y
position ofx
is enough.