UNPKG

ag-grid-enterprise

Version:

AG Grid Enterprise Features

102 lines 3.23 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.cubicRoots = void 0; /** * Finds the roots of a parametric linear equation in `t`, * where `t` lies in the interval of `[0,1]`. */ function linearRoot(a, b) { var t = -b / a; return a !== 0 && t >= 0 && t <= 1 ? [t] : []; } /** * Finds the roots of a parametric quadratic equation in `t`, * where `t` lies in the interval of `[0,1]`. */ function quadraticRoots(a, b, c) { if (a === 0) { return linearRoot(b, c); } var D = b * b - 4 * a * c; // The polynomial's discriminant. var roots = []; if (D === 0) { // A single real root. var t = -b / (2 * a); if (t >= 0 && t <= 1) { roots.push(t); } } else if (D > 0) { // A pair of distinct real roots. var rD = Math.sqrt(D); var t1 = (-b - rD) / (2 * a); var t2 = (-b + rD) / (2 * a); if (t1 >= 0 && t1 <= 1) { roots.push(t1); } if (t2 >= 0 && t2 <= 1) { roots.push(t2); } } // else -> Complex roots. return roots; } /** * Finds the roots of a parametric cubic equation in `t`, * where `t` lies in the interval of `[0,1]`. * Returns an array of parametric intersection locations along the cubic, * excluding out-of-bounds intersections (before or after the end point * or in the imaginary plane). * An adaptation of http://www.particleincell.com/blog/2013/cubic-line-intersection/ */ function cubicRoots(a, b, c, d) { if (a === 0) { return quadraticRoots(b, c, d); } var A = b / a; var B = c / a; var C = d / a; var Q = (3 * B - A * A) / 9; var R = (9 * A * B - 27 * C - 2 * A * A * A) / 54; var D = Q * Q * Q + R * R; // The polynomial's discriminant. var third = 1 / 3; var roots = []; if (D >= 0) { // Complex or duplicate roots. var rD = Math.sqrt(D); var S = Math.sign(R + rD) * Math.pow(Math.abs(R + rD), third); var T = Math.sign(R - rD) * Math.pow(Math.abs(R - rD), third); var Im = Math.abs((Math.sqrt(3) * (S - T)) / 2); // Complex part of the root pair. var t = -third * A + (S + T); // A real root. if (t >= 0 && t <= 1) { roots.push(t); } if (Im === 0) { var t_1 = -third * A - (S + T) / 2; // The real part of a complex root. if (t_1 >= 0 && t_1 <= 1) { roots.push(t_1); } } } else { // Distinct real roots. var theta = Math.acos(R / Math.sqrt(-Q * Q * Q)); var thirdA = third * A; var twoSqrtQ = 2 * Math.sqrt(-Q); var t1 = twoSqrtQ * Math.cos(third * theta) - thirdA; var t2 = twoSqrtQ * Math.cos(third * (theta + 2 * Math.PI)) - thirdA; var t3 = twoSqrtQ * Math.cos(third * (theta + 4 * Math.PI)) - thirdA; if (t1 >= 0 && t1 <= 1) { roots.push(t1); } if (t2 >= 0 && t2 <= 1) { roots.push(t2); } if (t3 >= 0 && t3 <= 1) { roots.push(t3); } } return roots; } exports.cubicRoots = cubicRoots; //# sourceMappingURL=polyRoots.js.map