@woosh/meep-engine
Version:
Pure JavaScript game engine. Fully featured and production ready.
35 lines (30 loc) • 1.1 kB
JavaScript
/**
* Calculates the integral (area under the curve) of a cubic hermite spline from 0 to t.
*
* @param {number} t normalized interpolation position in interval [0,1]
* @param {number} p0 first value
* @param {number} p1 second value
* @param {number} m0 first tangent
* @param {number} m1 second tangent
* @returns {number} The definite integral from 0 to t
*
* @see spline3_hermite_derivative
*/
export function spline3_hermite_integral(t, p0, p1, m0, m1) {
const t2 = t * t;
const t3 = t2 * t;
const t4 = t3 * t;
// Integral of (2t^3 - 3t^2 + 1)
// Result: 0.5t^4 - t^3 + t
const int_p0 = 0.5 * t4 - t3 + t;
// Integral of (t^3 - 2t^2 + t)
// Result: 0.25t^4 - (2/3)t^3 + 0.5t^2
const int_m0 = 0.25 * t4 - (2.0 / 3.0) * t3 + 0.5 * t2;
// Integral of (t^3 - t^2)
// Result: 0.25t^4 - (1/3)t^3
const int_m1 = 0.25 * t4 - (1.0 / 3.0) * t3;
// Integral of (3t^2 - 2t^3)
// Result: t^3 - 0.5t^4
const int_p1 = t3 - 0.5 * t4;
return int_p0 * p0 + int_m0 * m0 + int_m1 * m1 + int_p1 * p1;
}