@woosh/meep-engine
Version:
Pure JavaScript game engine. Fully featured and production ready.
30 lines (26 loc) • 1.02 kB
JavaScript
/**
* Adds polynomial `a` into `target` in place.
*
* target(x) := target(x) + a(x)
*
* Coefficient layout: index i is the coefficient of x^i (ascending). If
* `a_len > target_len`, the excess coefficients in `target` at indices
* [target_len, a_len) are first cleared to zero, so the returned length
* (max of the two input lengths) reflects valid coefficients.
*
* @param {number[]|Float64Array|Float32Array} target receives the sum, length >= max(target_len, a_len)
* @param {number} target_len current number of coefficients in target
* @param {number[]|Float64Array|Float32Array} a
* @param {number} a_len number of coefficients in a
* @returns {number} new logical length of target = max(target_len, a_len)
*/
export function polynomial_add_into(target, target_len, a, a_len) {
const n = a_len > target_len ? a_len : target_len;
for (let i = target_len; i < n; i++) {
target[i] = 0;
}
for (let i = 0; i < a_len; i++) {
target[i] += a[i];
}
return n;
}