source: node_modules/victory-vendor/lib-vendor/d3-shape/src/curve/natural.js

Last change on this file was a762898, checked in by istevanoska <ilinastevanoska@…>, 5 months ago

Added visualizations

  • Property mode set to 100644
File size: 1.8 KB
Line 
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.default = _default;
7function Natural(context) {
8 this._context = context;
9}
10Natural.prototype = {
11 areaStart: function () {
12 this._line = 0;
13 },
14 areaEnd: function () {
15 this._line = NaN;
16 },
17 lineStart: function () {
18 this._x = [];
19 this._y = [];
20 },
21 lineEnd: function () {
22 var x = this._x,
23 y = this._y,
24 n = x.length;
25 if (n) {
26 this._line ? this._context.lineTo(x[0], y[0]) : this._context.moveTo(x[0], y[0]);
27 if (n === 2) {
28 this._context.lineTo(x[1], y[1]);
29 } else {
30 var px = controlPoints(x),
31 py = controlPoints(y);
32 for (var i0 = 0, i1 = 1; i1 < n; ++i0, ++i1) {
33 this._context.bezierCurveTo(px[0][i0], py[0][i0], px[1][i0], py[1][i0], x[i1], y[i1]);
34 }
35 }
36 }
37 if (this._line || this._line !== 0 && n === 1) this._context.closePath();
38 this._line = 1 - this._line;
39 this._x = this._y = null;
40 },
41 point: function (x, y) {
42 this._x.push(+x);
43 this._y.push(+y);
44 }
45};
46
47// See https://www.particleincell.com/2012/bezier-splines/ for derivation.
48function controlPoints(x) {
49 var i,
50 n = x.length - 1,
51 m,
52 a = new Array(n),
53 b = new Array(n),
54 r = new Array(n);
55 a[0] = 0, b[0] = 2, r[0] = x[0] + 2 * x[1];
56 for (i = 1; i < n - 1; ++i) a[i] = 1, b[i] = 4, r[i] = 4 * x[i] + 2 * x[i + 1];
57 a[n - 1] = 2, b[n - 1] = 7, r[n - 1] = 8 * x[n - 1] + x[n];
58 for (i = 1; i < n; ++i) m = a[i] / b[i - 1], b[i] -= m, r[i] -= m * r[i - 1];
59 a[n - 1] = r[n - 1] / b[n - 1];
60 for (i = n - 2; i >= 0; --i) a[i] = (r[i] - a[i + 1]) / b[i];
61 b[n - 1] = (x[n] + a[n - 1]) / 2;
62 for (i = 0; i < n - 1; ++i) b[i] = 2 * x[i + 1] - a[i + 1];
63 return [a, b];
64}
65function _default(context) {
66 return new Natural(context);
67}
Note: See TracBrowser for help on using the repository browser.