From 55c7d6872aafad0748d305bb3cae28d79d9ab470 Mon Sep 17 00:00:00 2001 From: Evert Timberg Date: Sat, 14 Nov 2015 22:49:08 -0500 Subject: [PATCH] Prevent NaN results when previous, point, and next are all the same --- src/core/core.helpers.js | 30 ++++++++++++++++-------------- 1 file changed, 16 insertions(+), 14 deletions(-) diff --git a/src/core/core.helpers.js b/src/core/core.helpers.js index 1f19a5c0a..50d5c51ec 100644 --- a/src/core/core.helpers.js +++ b/src/core/core.helpers.js @@ -321,27 +321,29 @@ aliasPixel = helpers.aliasPixel = function(pixelWidth) { return (pixelWidth % 2 === 0) ? 0 : 0.5; }, - splineCurve = helpers.splineCurve = function(FirstPoint, MiddlePoint, AfterPoint, t) { + splineCurve = helpers.splineCurve = function(firstPoint, middlePoint, afterPoint, t) { //Props to Rob Spencer at scaled innovation for his post on splining between points //http://scaledinnovation.com/analytics/splines/aboutSplines.html // This function must also respect "skipped" points - var previous = FirstPoint, - current = MiddlePoint, - next = AfterPoint; + var previous = firstPoint.skip ? middlePoint : firstPoint, + current = middlePoint, + next = afterPoint.skip ? middlePoint : afterPoint; - if (previous.skip) { - previous = current; - } - if (next.skip) { - next = current; - } + var d01 = Math.sqrt(Math.pow(current.x - previous.x, 2) + Math.pow(current.y - previous.y, 2)); + var d12 = Math.sqrt(Math.pow(next.x - current.x, 2) + Math.pow(next.y - current.y, 2)); + + var s01 = d01 / (d01 + d12); + var s12 = d12 / (d01 + d12); + + // If all points are the same, s01 & s02 will be inf + s01 = isNaN(s01) ? 0 : s01; + s12 = isNaN(s12) ? 0 : s12; + + var fa = t * s01; // scaling factor for triangle Ta + var fb = t * s12; - var d01 = Math.sqrt(Math.pow(current.x - previous.x, 2) + Math.pow(current.y - previous.y, 2)), - d12 = Math.sqrt(Math.pow(next.x - current.x, 2) + Math.pow(next.y - current.y, 2)), - fa = t * d01 / (d01 + d12), // scaling factor for triangle Ta - fb = t * d12 / (d01 + d12); return { previous: { x: current.x - fa * (next.x - previous.x),