Skip to content

Commit fe6d0fd

Browse files
committed
"Bundle" interpolation for single-element arrays.
In future we may want to generate some kind of loop, but it's not clear what orientation such a loop should have, so perhaps a "non-line" like this is better as a default.
1 parent 2697a60 commit fe6d0fd

4 files changed

Lines changed: 38 additions & 30 deletions

File tree

d3.v2.js

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3547,19 +3547,21 @@ function d3_svg_lineBasisClosed(points) {
35473547
}
35483548

35493549
function d3_svg_lineBundle(points, tension) {
3550-
var n = points.length - 1,
3551-
x0 = points[0][0],
3552-
y0 = points[0][1],
3553-
dx = points[n][0] - x0,
3554-
dy = points[n][1] - y0,
3555-
i = -1,
3556-
p,
3557-
t;
3558-
while (++i <= n) {
3559-
p = points[i];
3560-
t = i / n;
3561-
p[0] = tension * p[0] + (1 - tension) * (x0 + t * dx);
3562-
p[1] = tension * p[1] + (1 - tension) * (y0 + t * dy);
3550+
var n = points.length - 1;
3551+
if (n) {
3552+
var x0 = points[0][0],
3553+
y0 = points[0][1],
3554+
dx = points[n][0] - x0,
3555+
dy = points[n][1] - y0,
3556+
i = -1,
3557+
p,
3558+
t;
3559+
while (++i <= n) {
3560+
p = points[i];
3561+
t = i / n;
3562+
p[0] = tension * p[0] + (1 - tension) * (x0 + t * dx);
3563+
p[1] = tension * p[1] + (1 - tension) * (y0 + t * dy);
3564+
}
35633565
}
35643566
return d3_svg_lineBasis(points);
35653567
}

d3.v2.min.js

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/svg/line.js

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -298,19 +298,21 @@ function d3_svg_lineBasisClosed(points) {
298298
}
299299

300300
function d3_svg_lineBundle(points, tension) {
301-
var n = points.length - 1,
302-
x0 = points[0][0],
303-
y0 = points[0][1],
304-
dx = points[n][0] - x0,
305-
dy = points[n][1] - y0,
306-
i = -1,
307-
p,
308-
t;
309-
while (++i <= n) {
310-
p = points[i];
311-
t = i / n;
312-
p[0] = tension * p[0] + (1 - tension) * (x0 + t * dx);
313-
p[1] = tension * p[1] + (1 - tension) * (y0 + t * dy);
301+
var n = points.length - 1;
302+
if (n) {
303+
var x0 = points[0][0],
304+
y0 = points[0][1],
305+
dx = points[n][0] - x0,
306+
dy = points[n][1] - y0,
307+
i = -1,
308+
p,
309+
t;
310+
while (++i <= n) {
311+
p = points[i];
312+
t = i / n;
313+
p[0] = tension * p[0] + (1 - tension) * (x0 + t * dx);
314+
p[1] = tension * p[1] + (1 - tension) * (y0 + t * dy);
315+
}
314316
}
315317
return d3_svg_lineBasis(points);
316318
}

test/svg/line-test.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,10 @@ suite.addBatch({
134134
"observes the specified tension": function(line) {
135135
var l = line().interpolate("bundle").tension(1);
136136
assert.pathEqual(l([[0, 0], [1, 1], [2, 0], [3, 1], [4, 0]]), line().interpolate("basis")([[0, 0], [1, 1], [2, 0], [3, 1], [4, 0]]));
137+
},
138+
"supports a single-element array": function(line) {
139+
var l = line().interpolate("bundle").tension(1);
140+
assert.pathEqual(l([[0, 0]]), "M0,0");
137141
}
138142
},
139143

0 commit comments

Comments
 (0)