Skip to content

Commit

Permalink
Add d3.piecewise.
Browse files Browse the repository at this point in the history
  • Loading branch information
mbostock committed May 7, 2018
1 parent a2d2c28 commit 96eb23d
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 0 deletions.
10 changes: 10 additions & 0 deletions README.md
Expand Up @@ -223,3 +223,13 @@ Returns a uniform nonrational B-spline interpolator through the specified array
<a href="#interpolateBasisClosed" name="interpolateBasisClosed">#</a> d3.<b>interpolateBasisClosed</b>(<i>values</i>) [<>](https://github.com/d3/d3-interpolate/blob/master/src/basisClosed.js "Source")

Returns a uniform nonrational B-spline interpolator through the specified array of *values*, which must be numbers. The control points are implicitly repeated such that the resulting one-dimensional spline has cyclical C² continuity when repeated around *t* in [0,1]. See also [d3.curveBasisClosed](https://github.com/d3/d3-shape#curveBasisClosed).

### Piecewise

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

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"]);
```
1 change: 1 addition & 0 deletions index.js
Expand Up @@ -14,4 +14,5 @@ export {default as interpolateHsl, hslLong as interpolateHslLong} from "./src/hs
export {default as interpolateLab} from "./src/lab";
export {default as interpolateHcl, hclLong as interpolateHclLong} from "./src/hcl";
export {default as interpolateCubehelix, cubehelixLong as interpolateCubehelixLong} from "./src/cubehelix";
export {default as piecewise} from "./src/piecewise";
export {default as quantize} from "./src/quantize";
8 changes: 8 additions & 0 deletions src/piecewise.js
@@ -0,0 +1,8 @@
export default function piecewise(interpolate, values) {
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) {
var i = Math.max(0, Math.min(n - 1, Math.floor(t *= n)));
return I[i](t - i);
};
}

0 comments on commit 96eb23d

Please sign in to comment.