# particle - interpolate

I want to make a particle that interpolates in Alpha and velocity.

public class Particle extends GameObject implements Updateable
{

int time;

int Alpha;

double dx;

double dy;

int[][] alphaEnvelope;

double[][] speedEnvelope;

@Override
public void update()
{
time++;

}

}


this is my particle class.

alphaEnvelope[x][y] changes the Alpha over time where X is the time and Y is the Alpha Value.

For example:

[1][100]

[60][70]

[70][0]

How do I interpolate between these values fluently?

• there are always only and only 3 points known? – dnk drone.vs.drones Aug 27 '15 at 10:39
• no, the Array is different for every particle – Raildex Aug 27 '15 at 11:08
• What kind of interpolation do you want/need? If you are not sure, you can show it with a picture. – wondra Aug 27 '15 at 16:17
• A linear interpolation will do it for the moment. I have an array of X Values and an Array of Y values. my X0 increases over time and I want to get the Y values for X0. i.imgur.com/pgUP32w.png My code now looks like this: It does work a bit, but if the second Key is selected, it does not interpolate anymore but the alpha changes instantly. pastebin.com/mmnPUr8s – Raildex Aug 27 '15 at 16:32

Linear interpolation you can do with following code:

int l = 0;
for(int i = 0; i < keys.Length; i++)
{
if(time > keys[i]) l = i;
}
double interval_len = keys[l + 1] - keys[l]; //r = l + 1
double k2_a = (time - keys[l]) / interval_len; //k1_a = 1 - k2_a
return k2_a * values[l+1] + (1 - k2_a) * values[l];


It is very similar to your code - you were on the right track. Code explanation: first we find between which keys the time lies, then we find the interval length, after that we subtract left value from time to get "local time on interval". And lastly we find "how much close" it is to right boundary by dividing local time by interval length. We multiply right value by the result and finally we multiply the left value by complement and sum it for desired result.
If you wanted more smooth values you could for example interpolate by cubic spline solving system of linear equations (not hard to find, you will be likely looking for chord parameterization and natural spline bounding conditions).

You can try some polinomial interpolation : i.e lagrangian interpolation

In your example it becomes (if I calculated it right ):

L(x) = 100 * ((x-60)/(1-60)) * ((x-70)/(1-70)) +
70 * ((x-1)/(60-1)) * ((x-70)/(60-70)) +
0 * ((x-1)/(70-1)) * ((x-60)/(70-60)) +

• A word of warning would be in order for this answer: lagrangian interpolation is a global interpolation and it is not very stable - it can give you really crazy results, including values over 100 or below 0 for your kind of inputs. – wondra Aug 27 '15 at 16:20