1 |
|
2 | (function (global, factory) {
|
3 | typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
|
4 | typeof define === 'function' && define.amd ? define(['exports'], factory) :
|
5 | (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.d3 = global.d3 || {}));
|
6 | })(this, (function (exports) { 'use strict';
|
7 |
|
8 | const pi = Math.PI,
|
9 | tau = 2 * pi,
|
10 | epsilon = 1e-6,
|
11 | tauEpsilon = tau - epsilon;
|
12 |
|
13 | function append(strings) {
|
14 | this._ += strings[0];
|
15 | for (let i = 1, n = strings.length; i < n; ++i) {
|
16 | this._ += arguments[i] + strings[i];
|
17 | }
|
18 | }
|
19 |
|
20 | function appendRound(digits) {
|
21 | let d = Math.floor(digits);
|
22 | if (!(d >= 0)) throw new Error(`invalid digits: ${digits}`);
|
23 | if (d > 15) return append;
|
24 | const k = 10 ** d;
|
25 | return function(strings) {
|
26 | this._ += strings[0];
|
27 | for (let i = 1, n = strings.length; i < n; ++i) {
|
28 | this._ += Math.round(arguments[i] * k) / k + strings[i];
|
29 | }
|
30 | };
|
31 | }
|
32 |
|
33 | class Path {
|
34 | constructor(digits) {
|
35 | this._x0 = this._y0 =
|
36 | this._x1 = this._y1 = null;
|
37 | this._ = "";
|
38 | this._append = digits == null ? append : appendRound(digits);
|
39 | }
|
40 | moveTo(x, y) {
|
41 | this._append`M${this._x0 = this._x1 = +x},${this._y0 = this._y1 = +y}`;
|
42 | }
|
43 | closePath() {
|
44 | if (this._x1 !== null) {
|
45 | this._x1 = this._x0, this._y1 = this._y0;
|
46 | this._append`Z`;
|
47 | }
|
48 | }
|
49 | lineTo(x, y) {
|
50 | this._append`L${this._x1 = +x},${this._y1 = +y}`;
|
51 | }
|
52 | quadraticCurveTo(x1, y1, x, y) {
|
53 | this._append`Q${+x1},${+y1},${this._x1 = +x},${this._y1 = +y}`;
|
54 | }
|
55 | bezierCurveTo(x1, y1, x2, y2, x, y) {
|
56 | this._append`C${+x1},${+y1},${+x2},${+y2},${this._x1 = +x},${this._y1 = +y}`;
|
57 | }
|
58 | arcTo(x1, y1, x2, y2, r) {
|
59 | x1 = +x1, y1 = +y1, x2 = +x2, y2 = +y2, r = +r;
|
60 |
|
61 |
|
62 | if (r < 0) throw new Error(`negative radius: ${r}`);
|
63 |
|
64 | let x0 = this._x1,
|
65 | y0 = this._y1,
|
66 | x21 = x2 - x1,
|
67 | y21 = y2 - y1,
|
68 | x01 = x0 - x1,
|
69 | y01 = y0 - y1,
|
70 | l01_2 = x01 * x01 + y01 * y01;
|
71 |
|
72 |
|
73 | if (this._x1 === null) {
|
74 | this._append`M${this._x1 = x1},${this._y1 = y1}`;
|
75 | }
|
76 |
|
77 |
|
78 | else if (!(l01_2 > epsilon));
|
79 |
|
80 |
|
81 |
|
82 |
|
83 | else if (!(Math.abs(y01 * x21 - y21 * x01) > epsilon) || !r) {
|
84 | this._append`L${this._x1 = x1},${this._y1 = y1}`;
|
85 | }
|
86 |
|
87 |
|
88 | else {
|
89 | let x20 = x2 - x0,
|
90 | y20 = y2 - y0,
|
91 | l21_2 = x21 * x21 + y21 * y21,
|
92 | l20_2 = x20 * x20 + y20 * y20,
|
93 | l21 = Math.sqrt(l21_2),
|
94 | l01 = Math.sqrt(l01_2),
|
95 | l = r * Math.tan((pi - Math.acos((l21_2 + l01_2 - l20_2) / (2 * l21 * l01))) / 2),
|
96 | t01 = l / l01,
|
97 | t21 = l / l21;
|
98 |
|
99 |
|
100 | if (Math.abs(t01 - 1) > epsilon) {
|
101 | this._append`L${x1 + t01 * x01},${y1 + t01 * y01}`;
|
102 | }
|
103 |
|
104 | this._append`A${r},${r},0,0,${+(y01 * x20 > x01 * y20)},${this._x1 = x1 + t21 * x21},${this._y1 = y1 + t21 * y21}`;
|
105 | }
|
106 | }
|
107 | arc(x, y, r, a0, a1, ccw) {
|
108 | x = +x, y = +y, r = +r, ccw = !!ccw;
|
109 |
|
110 |
|
111 | if (r < 0) throw new Error(`negative radius: ${r}`);
|
112 |
|
113 | let dx = r * Math.cos(a0),
|
114 | dy = r * Math.sin(a0),
|
115 | x0 = x + dx,
|
116 | y0 = y + dy,
|
117 | cw = 1 ^ ccw,
|
118 | da = ccw ? a0 - a1 : a1 - a0;
|
119 |
|
120 |
|
121 | if (this._x1 === null) {
|
122 | this._append`M${x0},${y0}`;
|
123 | }
|
124 |
|
125 |
|
126 | else if (Math.abs(this._x1 - x0) > epsilon || Math.abs(this._y1 - y0) > epsilon) {
|
127 | this._append`L${x0},${y0}`;
|
128 | }
|
129 |
|
130 |
|
131 | if (!r) return;
|
132 |
|
133 |
|
134 | if (da < 0) da = da % tau + tau;
|
135 |
|
136 |
|
137 | if (da > tauEpsilon) {
|
138 | this._append`A${r},${r},0,1,${cw},${x - dx},${y - dy}A${r},${r},0,1,${cw},${this._x1 = x0},${this._y1 = y0}`;
|
139 | }
|
140 |
|
141 |
|
142 | else if (da > epsilon) {
|
143 | this._append`A${r},${r},0,${+(da >= pi)},${cw},${this._x1 = x + r * Math.cos(a1)},${this._y1 = y + r * Math.sin(a1)}`;
|
144 | }
|
145 | }
|
146 | rect(x, y, w, h) {
|
147 | this._append`M${this._x0 = this._x1 = +x},${this._y0 = this._y1 = +y}h${w = +w}v${+h}h${-w}Z`;
|
148 | }
|
149 | toString() {
|
150 | return this._;
|
151 | }
|
152 | }
|
153 |
|
154 | function path() {
|
155 | return new Path;
|
156 | }
|
157 |
|
158 |
|
159 | path.prototype = Path.prototype;
|
160 |
|
161 | function pathRound(digits = 3) {
|
162 | return new Path(+digits);
|
163 | }
|
164 |
|
165 | exports.Path = Path;
|
166 | exports.path = path;
|
167 | exports.pathRound = pathRound;
|
168 |
|
169 | }));
|