Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Piecewise default interpolator #90

Merged
merged 1 commit into from Jul 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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();
});