From 7009fc3ea020ba9745c7594d03346ebfdd61864e Mon Sep 17 00:00:00 2001 From: Jan Date: Thu, 16 Jun 2016 13:13:16 +0200 Subject: [PATCH] Add check of value range in solveCubic Performance of `Curve.solveCubic()` can be improved by first checking if the specified value is within the curve's value range. If it is outside the range, the expensive call to `Numerical.solveCubic()` is not necessary. --- src/path/Curve.js | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/path/Curve.js b/src/path/Curve.js index ae7286f3..3a1fd518 100644 --- a/src/path/Curve.js +++ b/src/path/Curve.js @@ -628,8 +628,13 @@ statics: /** @lends Curve */{ var p1 = v[coord], c1 = v[coord + 2], c2 = v[coord + 4], - p2 = v[coord + 6], - c = 3 * (c1 - p1), + p2 = v[coord + 6]; + if (p1 < val && p2 < val && c1 < val && c2 < val + || p1 > val && p2 > val && c1 > val && c2 > val) { + // If val is outside the curve values, no solution is possible. + return 0; + } + var c = 3 * (c1 - p1), b = 3 * (c2 - c1) - c, a = p2 - p1 - c - b; return Numerical.solveCubic(a, b, c, p1 - val, roots, min, max);