Line data Source code
1 : /**
2 : * Interpolates the value of a point that is located between four known equidistant points with given values by
3 : * constructing a polynomial of degree three that goes through all points.
4 : * Computes a cardinal or Catmull-Rom spline.
5 : *
6 : * @param {number} tension - determines how sharply the curve bends at the given points.
7 : * @param {number} x - Location of the point to interpolate, relative to p1
8 : */
9 : function cubicInterpolation(tension, x, p0, p1, p2, p3)
10 : {
11 0 : let P = -tension * p0 + (2 - tension) * p1 + (tension - 2) * p2 + tension * p3;
12 0 : let Q = 2 * tension * p0 + (tension - 3) * p1 + (3 - 2 * tension) * p2 - tension * p3;
13 0 : let R = -tension * p0 + tension * p2;
14 0 : let S = p1;
15 :
16 0 : return ((P * x + Q) * x + R) * x + S;
17 : }
18 :
19 : /**
20 : * Two dimensional interpolation within a square grid using a polynomial of degree three.
21 : *
22 : * @param {Vector2D} position - Location of the point to interpolate, relative to p11
23 : */
24 : function bicubicInterpolation
25 : (
26 : position,
27 : p00, p01, p02, p03,
28 : p10, p11, p12, p13,
29 : p20, p21, p22, p23,
30 : p30, p31, p32, p33
31 : )
32 : {
33 0 : let tension = 0.5;
34 0 : return cubicInterpolation(
35 : tension,
36 : position.x,
37 : cubicInterpolation(tension, position.y, p00, p01, p02, p03),
38 : cubicInterpolation(tension, position.y, p10, p11, p12, p13),
39 : cubicInterpolation(tension, position.y, p20, p21, p22, p23),
40 : cubicInterpolation(tension, position.y, p30, p31, p32, p33));
41 : }
|