Skip to content

Commit

Permalink
Merge pull request #90 from d3/piecewise-default-interpolator-80
Browse files Browse the repository at this point in the history
Piecewise default interpolator
  • Loading branch information
Fil committed Jul 24, 2020
2 parents 57e87dc + 0c80cbc commit 17d8bcb
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 1 deletion.
4 changes: 3 additions & 1 deletion README.md
Expand Up @@ -247,10 +247,12 @@ Returns a uniform nonrational B-spline interpolator through the specified array

### Piecewise

<a name="piecewise" href="#piecewise">#</a> d3.<b>piecewise</b>(<i>interpolate</i>, <i>values</i>) · [Source](https://github.com/d3/d3-interpolate/blob/master/src/piecewise.js), [Examples](https://observablehq.com/@d3/d3-piecewise)
<a name="piecewise" href="#piecewise">#</a> d3.<b>piecewise</b>([<i>interpolate</i>, ]<i>values</i>) · [Source](https://github.com/d3/d3-interpolate/blob/master/src/piecewise.js), [Examples](https://observablehq.com/@d3/d3-piecewise)

Returns a piecewise interpolator, composing interpolators for each adjacent pair of *values*. The returned interpolator maps *t* in [0, 1 / (*n* - 1)] to *interpolate*(*values*[0], *values*[1]), *t* in [1 / (*n* - 1), 2 / (*n* - 1)] to *interpolate*(*values*[1], *values*[2]), and so on, where *n* = *values*.length. In effect, this is a lightweight [linear scale](https://github.com/d3/d3-scale/blob/master/README.md#linear-scales). For example, to blend through red, green and blue:

```js
var interpolate = d3.piecewise(d3.interpolateRgb.gamma(2.2), ["red", "green", "blue"]);
```

If *interpolate* is not specified, defaults to [d3.interpolate](#interpolate).
3 changes: 3 additions & 0 deletions src/piecewise.js
@@ -1,4 +1,7 @@
import {default as value} from "./value.js";

export default function piecewise(interpolate, values) {
if (values === undefined) values = interpolate, interpolate = value;
var i = 0, n = values.length - 1, v = values[0], I = new Array(n < 0 ? 0 : n);
while (i < n) I[i] = interpolate(v, v = values[++i]);
return function(t) {
Expand Down
38 changes: 38 additions & 0 deletions test/piecewise-test.js
@@ -0,0 +1,38 @@
var tape = require("tape"),
interpolate = require("../");

tape("piecewise(interpolate, values)(t) returns the expected values", function(test) {
var i = interpolate.piecewise(interpolate.interpolate, [0,2,10]);
test.strictEqual(i(-1), -4);
test.strictEqual(i(0), 0);
test.strictEqual(i(0.19), 0.76);
test.strictEqual(i(0.21), 0.84);
test.strictEqual(i(0.5), 2);
test.strictEqual(i(0.75), 6);
test.strictEqual(i(1), 10);
test.end();
});

tape("piecewise(values) uses the default interpolator", function(test) {
var i = interpolate.piecewise([0,2,10]);
test.strictEqual(i(-1), -4);
test.strictEqual(i(0), 0);
test.strictEqual(i(0.19), 0.76);
test.strictEqual(i(0.21), 0.84);
test.strictEqual(i(0.5), 2);
test.strictEqual(i(0.75), 6);
test.strictEqual(i(1), 10);
test.end();
});

tape("piecewise(values) uses the default interpolator/2", function(test) {
var i = interpolate.piecewise(["a0","a2","a10"]);
test.strictEqual(i(-1), "a-4");
test.strictEqual(i(0), "a0");
test.strictEqual(i(0.19), "a0.76");
test.strictEqual(i(0.21), "a0.84");
test.strictEqual(i(0.5), "a2");
test.strictEqual(i(0.75), "a6");
test.strictEqual(i(1), "a10");
test.end();
});

0 comments on commit 17d8bcb

Please sign in to comment.