Prevent NaN results when previous, point, and next are all the same

This commit is contained in:
Evert Timberg 2015-11-14 22:49:08 -05:00
parent 3bd9120a3a
commit 55c7d6872a

View File

@ -321,27 +321,29 @@
aliasPixel = helpers.aliasPixel = function(pixelWidth) { aliasPixel = helpers.aliasPixel = function(pixelWidth) {
return (pixelWidth % 2 === 0) ? 0 : 0.5; 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 //Props to Rob Spencer at scaled innovation for his post on splining between points
//http://scaledinnovation.com/analytics/splines/aboutSplines.html //http://scaledinnovation.com/analytics/splines/aboutSplines.html
// This function must also respect "skipped" points // This function must also respect "skipped" points
var previous = FirstPoint, var previous = firstPoint.skip ? middlePoint : firstPoint,
current = MiddlePoint, current = middlePoint,
next = AfterPoint; next = afterPoint.skip ? middlePoint : afterPoint;
if (previous.skip) { var d01 = Math.sqrt(Math.pow(current.x - previous.x, 2) + Math.pow(current.y - previous.y, 2));
previous = current; var d12 = Math.sqrt(Math.pow(next.x - current.x, 2) + Math.pow(next.y - current.y, 2));
}
if (next.skip) { var s01 = d01 / (d01 + d12);
next = current; 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 { return {
previous: { previous: {
x: current.x - fa * (next.x - previous.x), x: current.x - fa * (next.x - previous.x),