source: node_modules/victory-vendor/lib-vendor/d3-shape/src/curve/monotone.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: 3.4 KB
Line 
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.monotoneX = monotoneX;
7exports.monotoneY = monotoneY;
8function sign(x) {
9 return x < 0 ? -1 : 1;
10}
11
12// Calculate the slopes of the tangents (Hermite-type interpolation) based on
13// the following paper: Steffen, M. 1990. A Simple Method for Monotonic
14// Interpolation in One Dimension. Astronomy and Astrophysics, Vol. 239, NO.
15// NOV(II), P. 443, 1990.
16function slope3(that, x2, y2) {
17 var h0 = that._x1 - that._x0,
18 h1 = x2 - that._x1,
19 s0 = (that._y1 - that._y0) / (h0 || h1 < 0 && -0),
20 s1 = (y2 - that._y1) / (h1 || h0 < 0 && -0),
21 p = (s0 * h1 + s1 * h0) / (h0 + h1);
22 return (sign(s0) + sign(s1)) * Math.min(Math.abs(s0), Math.abs(s1), 0.5 * Math.abs(p)) || 0;
23}
24
25// Calculate a one-sided slope.
26function slope2(that, t) {
27 var h = that._x1 - that._x0;
28 return h ? (3 * (that._y1 - that._y0) / h - t) / 2 : t;
29}
30
31// According to https://en.wikipedia.org/wiki/Cubic_Hermite_spline#Representations
32// "you can express cubic Hermite interpolation in terms of cubic Bézier curves
33// with respect to the four values p0, p0 + m0 / 3, p1 - m1 / 3, p1".
34function point(that, t0, t1) {
35 var x0 = that._x0,
36 y0 = that._y0,
37 x1 = that._x1,
38 y1 = that._y1,
39 dx = (x1 - x0) / 3;
40 that._context.bezierCurveTo(x0 + dx, y0 + dx * t0, x1 - dx, y1 - dx * t1, x1, y1);
41}
42function MonotoneX(context) {
43 this._context = context;
44}
45MonotoneX.prototype = {
46 areaStart: function () {
47 this._line = 0;
48 },
49 areaEnd: function () {
50 this._line = NaN;
51 },
52 lineStart: function () {
53 this._x0 = this._x1 = this._y0 = this._y1 = this._t0 = NaN;
54 this._point = 0;
55 },
56 lineEnd: function () {
57 switch (this._point) {
58 case 2:
59 this._context.lineTo(this._x1, this._y1);
60 break;
61 case 3:
62 point(this, this._t0, slope2(this, this._t0));
63 break;
64 }
65 if (this._line || this._line !== 0 && this._point === 1) this._context.closePath();
66 this._line = 1 - this._line;
67 },
68 point: function (x, y) {
69 var t1 = NaN;
70 x = +x, y = +y;
71 if (x === this._x1 && y === this._y1) return; // Ignore coincident points.
72 switch (this._point) {
73 case 0:
74 this._point = 1;
75 this._line ? this._context.lineTo(x, y) : this._context.moveTo(x, y);
76 break;
77 case 1:
78 this._point = 2;
79 break;
80 case 2:
81 this._point = 3;
82 point(this, slope2(this, t1 = slope3(this, x, y)), t1);
83 break;
84 default:
85 point(this, this._t0, t1 = slope3(this, x, y));
86 break;
87 }
88 this._x0 = this._x1, this._x1 = x;
89 this._y0 = this._y1, this._y1 = y;
90 this._t0 = t1;
91 }
92};
93function MonotoneY(context) {
94 this._context = new ReflectContext(context);
95}
96(MonotoneY.prototype = Object.create(MonotoneX.prototype)).point = function (x, y) {
97 MonotoneX.prototype.point.call(this, y, x);
98};
99function ReflectContext(context) {
100 this._context = context;
101}
102ReflectContext.prototype = {
103 moveTo: function (x, y) {
104 this._context.moveTo(y, x);
105 },
106 closePath: function () {
107 this._context.closePath();
108 },
109 lineTo: function (x, y) {
110 this._context.lineTo(y, x);
111 },
112 bezierCurveTo: function (x1, y1, x2, y2, x, y) {
113 this._context.bezierCurveTo(y1, x1, y2, x2, y, x);
114 }
115};
116function monotoneX(context) {
117 return new MonotoneX(context);
118}
119function monotoneY(context) {
120 return new MonotoneY(context);
121}
Note: See TracBrowser for help on using the repository browser.