From 3f11c4ec2a707f877d2b5c35642e15d3c9809e17 Mon Sep 17 00:00:00 2001 From: MichaelZinsmaier Date: Sun, 23 Sep 2012 20:01:04 +0200 Subject: [PATCH] added saddle detection which should result in nicer curves and hopefully does not lead to new problems --- curvedLines.js | 51 +++++++++++++++++++++++++++++++++++++------------- 1 file changed, 38 insertions(+), 13 deletions(-) diff --git a/curvedLines.js b/curvedLines.js index 0d44da4..ed3babb 100644 --- a/curvedLines.js +++ b/curvedLines.js @@ -262,41 +262,66 @@ //if fit option is selected additional datapoints get inserted before the curve calculations in nergal.dev s code. function calculateCurvePoints(data, curvedLinesOptions) { - var num = curvedLinesOptions.curvePointFactor * data.length; + var num = curvedLinesOptions.curvePointFactor * data.length; var xdata = new Array; var ydata = new Array; + + var X = 0; + var Y = 1; if (curvedLinesOptions.fit) { //insert a point before and after the "real" data point to force the line - //to have a max,min at the data point + //to have a max,min at the data point however only if it is a lowest or highest point of the + //curve => avoid saddles var neigh = curvedLinesOptions.fitPointDist; var j = 0; for (var i = 0; i < data.length; i++) { - //smooth front - xdata[j] = data[i][0] - 0.1; + var front = new Array; + var back = new Array; + //smooth front + front[X] = data[i][X] - 0.1; if (i > 0) { - ydata[j] = data[i-1][1] * neigh + data[i][1] * (1 - neigh); + front[Y] = data[i-1][Y] * neigh + data[i][Y] * (1 - neigh); } else { - ydata[j] = data[i][1]; + front[Y] = data[i][Y]; } - j++; - xdata[j] = data[i][0]; - ydata[j] = data[i][1]; - j++; //smooth back - xdata[j] = data[i][0] + 0.1; + back[X] = data[i][X] + 0.1; if ((i + 1) < data.length) { - ydata[j] = data[i+1][1] * neigh + data[i][1] * (1 - neigh); + back[Y] = data[i+1][Y] * neigh + data[i][Y] * (1 - neigh); } else { + back[Y] = data[i][Y]; + } + + //test for a saddle + if ((front[Y] <= data[i][Y] && back[Y] <= data[i][Y]) || //max or partial horizontal + (front[Y] >= data[i][Y] && back[Y] >= data[i][Y])) { //min or partial horizontal + + //add curve points + xdata[j] = front[X]; + ydata[j] = front[Y]; + j++; + + xdata[j] = data[i][0]; + ydata[j] = data[i][1]; + j++; + + xdata[j] = back[X]; + ydata[j] = back[Y]; + j++; + } else { //saddle + //use original point only + xdata[j] = data[i][0]; ydata[j] = data[i][1]; + j++; } - j++; + } } else { //just use the datapoints