source: node_modules/victory-vendor/lib-vendor/d3-shape/src/arc.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: 9.2 KB
Line 
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.default = _default;
7var _index = require("../../../lib-vendor/d3-path/src/index.js");
8var _constant = _interopRequireDefault(require("./constant.js"));
9var _math = require("./math.js");
10function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
11function arcInnerRadius(d) {
12 return d.innerRadius;
13}
14function arcOuterRadius(d) {
15 return d.outerRadius;
16}
17function arcStartAngle(d) {
18 return d.startAngle;
19}
20function arcEndAngle(d) {
21 return d.endAngle;
22}
23function arcPadAngle(d) {
24 return d && d.padAngle; // Note: optional!
25}
26function intersect(x0, y0, x1, y1, x2, y2, x3, y3) {
27 var x10 = x1 - x0,
28 y10 = y1 - y0,
29 x32 = x3 - x2,
30 y32 = y3 - y2,
31 t = y32 * x10 - x32 * y10;
32 if (t * t < _math.epsilon) return;
33 t = (x32 * (y0 - y2) - y32 * (x0 - x2)) / t;
34 return [x0 + t * x10, y0 + t * y10];
35}
36
37// Compute perpendicular offset line of length rc.
38// http://mathworld.wolfram.com/Circle-LineIntersection.html
39function cornerTangents(x0, y0, x1, y1, r1, rc, cw) {
40 var x01 = x0 - x1,
41 y01 = y0 - y1,
42 lo = (cw ? rc : -rc) / (0, _math.sqrt)(x01 * x01 + y01 * y01),
43 ox = lo * y01,
44 oy = -lo * x01,
45 x11 = x0 + ox,
46 y11 = y0 + oy,
47 x10 = x1 + ox,
48 y10 = y1 + oy,
49 x00 = (x11 + x10) / 2,
50 y00 = (y11 + y10) / 2,
51 dx = x10 - x11,
52 dy = y10 - y11,
53 d2 = dx * dx + dy * dy,
54 r = r1 - rc,
55 D = x11 * y10 - x10 * y11,
56 d = (dy < 0 ? -1 : 1) * (0, _math.sqrt)((0, _math.max)(0, r * r * d2 - D * D)),
57 cx0 = (D * dy - dx * d) / d2,
58 cy0 = (-D * dx - dy * d) / d2,
59 cx1 = (D * dy + dx * d) / d2,
60 cy1 = (-D * dx + dy * d) / d2,
61 dx0 = cx0 - x00,
62 dy0 = cy0 - y00,
63 dx1 = cx1 - x00,
64 dy1 = cy1 - y00;
65
66 // Pick the closer of the two intersection points.
67 // TODO Is there a faster way to determine which intersection to use?
68 if (dx0 * dx0 + dy0 * dy0 > dx1 * dx1 + dy1 * dy1) cx0 = cx1, cy0 = cy1;
69 return {
70 cx: cx0,
71 cy: cy0,
72 x01: -ox,
73 y01: -oy,
74 x11: cx0 * (r1 / r - 1),
75 y11: cy0 * (r1 / r - 1)
76 };
77}
78function _default() {
79 var innerRadius = arcInnerRadius,
80 outerRadius = arcOuterRadius,
81 cornerRadius = (0, _constant.default)(0),
82 padRadius = null,
83 startAngle = arcStartAngle,
84 endAngle = arcEndAngle,
85 padAngle = arcPadAngle,
86 context = null;
87 function arc() {
88 var buffer,
89 r,
90 r0 = +innerRadius.apply(this, arguments),
91 r1 = +outerRadius.apply(this, arguments),
92 a0 = startAngle.apply(this, arguments) - _math.halfPi,
93 a1 = endAngle.apply(this, arguments) - _math.halfPi,
94 da = (0, _math.abs)(a1 - a0),
95 cw = a1 > a0;
96 if (!context) context = buffer = (0, _index.path)();
97
98 // Ensure that the outer radius is always larger than the inner radius.
99 if (r1 < r0) r = r1, r1 = r0, r0 = r;
100
101 // Is it a point?
102 if (!(r1 > _math.epsilon)) context.moveTo(0, 0);
103
104 // Or is it a circle or annulus?
105 else if (da > _math.tau - _math.epsilon) {
106 context.moveTo(r1 * (0, _math.cos)(a0), r1 * (0, _math.sin)(a0));
107 context.arc(0, 0, r1, a0, a1, !cw);
108 if (r0 > _math.epsilon) {
109 context.moveTo(r0 * (0, _math.cos)(a1), r0 * (0, _math.sin)(a1));
110 context.arc(0, 0, r0, a1, a0, cw);
111 }
112 }
113
114 // Or is it a circular or annular sector?
115 else {
116 var a01 = a0,
117 a11 = a1,
118 a00 = a0,
119 a10 = a1,
120 da0 = da,
121 da1 = da,
122 ap = padAngle.apply(this, arguments) / 2,
123 rp = ap > _math.epsilon && (padRadius ? +padRadius.apply(this, arguments) : (0, _math.sqrt)(r0 * r0 + r1 * r1)),
124 rc = (0, _math.min)((0, _math.abs)(r1 - r0) / 2, +cornerRadius.apply(this, arguments)),
125 rc0 = rc,
126 rc1 = rc,
127 t0,
128 t1;
129
130 // Apply padding? Note that since r1 ≥ r0, da1 ≥ da0.
131 if (rp > _math.epsilon) {
132 var p0 = (0, _math.asin)(rp / r0 * (0, _math.sin)(ap)),
133 p1 = (0, _math.asin)(rp / r1 * (0, _math.sin)(ap));
134 if ((da0 -= p0 * 2) > _math.epsilon) p0 *= cw ? 1 : -1, a00 += p0, a10 -= p0;else da0 = 0, a00 = a10 = (a0 + a1) / 2;
135 if ((da1 -= p1 * 2) > _math.epsilon) p1 *= cw ? 1 : -1, a01 += p1, a11 -= p1;else da1 = 0, a01 = a11 = (a0 + a1) / 2;
136 }
137 var x01 = r1 * (0, _math.cos)(a01),
138 y01 = r1 * (0, _math.sin)(a01),
139 x10 = r0 * (0, _math.cos)(a10),
140 y10 = r0 * (0, _math.sin)(a10);
141
142 // Apply rounded corners?
143 if (rc > _math.epsilon) {
144 var x11 = r1 * (0, _math.cos)(a11),
145 y11 = r1 * (0, _math.sin)(a11),
146 x00 = r0 * (0, _math.cos)(a00),
147 y00 = r0 * (0, _math.sin)(a00),
148 oc;
149
150 // Restrict the corner radius according to the sector angle.
151 if (da < _math.pi && (oc = intersect(x01, y01, x00, y00, x11, y11, x10, y10))) {
152 var ax = x01 - oc[0],
153 ay = y01 - oc[1],
154 bx = x11 - oc[0],
155 by = y11 - oc[1],
156 kc = 1 / (0, _math.sin)((0, _math.acos)((ax * bx + ay * by) / ((0, _math.sqrt)(ax * ax + ay * ay) * (0, _math.sqrt)(bx * bx + by * by))) / 2),
157 lc = (0, _math.sqrt)(oc[0] * oc[0] + oc[1] * oc[1]);
158 rc0 = (0, _math.min)(rc, (r0 - lc) / (kc - 1));
159 rc1 = (0, _math.min)(rc, (r1 - lc) / (kc + 1));
160 }
161 }
162
163 // Is the sector collapsed to a line?
164 if (!(da1 > _math.epsilon)) context.moveTo(x01, y01);
165
166 // Does the sector’s outer ring have rounded corners?
167 else if (rc1 > _math.epsilon) {
168 t0 = cornerTangents(x00, y00, x01, y01, r1, rc1, cw);
169 t1 = cornerTangents(x11, y11, x10, y10, r1, rc1, cw);
170 context.moveTo(t0.cx + t0.x01, t0.cy + t0.y01);
171
172 // Have the corners merged?
173 if (rc1 < rc) context.arc(t0.cx, t0.cy, rc1, (0, _math.atan2)(t0.y01, t0.x01), (0, _math.atan2)(t1.y01, t1.x01), !cw);
174
175 // Otherwise, draw the two corners and the ring.
176 else {
177 context.arc(t0.cx, t0.cy, rc1, (0, _math.atan2)(t0.y01, t0.x01), (0, _math.atan2)(t0.y11, t0.x11), !cw);
178 context.arc(0, 0, r1, (0, _math.atan2)(t0.cy + t0.y11, t0.cx + t0.x11), (0, _math.atan2)(t1.cy + t1.y11, t1.cx + t1.x11), !cw);
179 context.arc(t1.cx, t1.cy, rc1, (0, _math.atan2)(t1.y11, t1.x11), (0, _math.atan2)(t1.y01, t1.x01), !cw);
180 }
181 }
182
183 // Or is the outer ring just a circular arc?
184 else context.moveTo(x01, y01), context.arc(0, 0, r1, a01, a11, !cw);
185
186 // Is there no inner ring, and it’s a circular sector?
187 // Or perhaps it’s an annular sector collapsed due to padding?
188 if (!(r0 > _math.epsilon) || !(da0 > _math.epsilon)) context.lineTo(x10, y10);
189
190 // Does the sector’s inner ring (or point) have rounded corners?
191 else if (rc0 > _math.epsilon) {
192 t0 = cornerTangents(x10, y10, x11, y11, r0, -rc0, cw);
193 t1 = cornerTangents(x01, y01, x00, y00, r0, -rc0, cw);
194 context.lineTo(t0.cx + t0.x01, t0.cy + t0.y01);
195
196 // Have the corners merged?
197 if (rc0 < rc) context.arc(t0.cx, t0.cy, rc0, (0, _math.atan2)(t0.y01, t0.x01), (0, _math.atan2)(t1.y01, t1.x01), !cw);
198
199 // Otherwise, draw the two corners and the ring.
200 else {
201 context.arc(t0.cx, t0.cy, rc0, (0, _math.atan2)(t0.y01, t0.x01), (0, _math.atan2)(t0.y11, t0.x11), !cw);
202 context.arc(0, 0, r0, (0, _math.atan2)(t0.cy + t0.y11, t0.cx + t0.x11), (0, _math.atan2)(t1.cy + t1.y11, t1.cx + t1.x11), cw);
203 context.arc(t1.cx, t1.cy, rc0, (0, _math.atan2)(t1.y11, t1.x11), (0, _math.atan2)(t1.y01, t1.x01), !cw);
204 }
205 }
206
207 // Or is the inner ring just a circular arc?
208 else context.arc(0, 0, r0, a10, a00, cw);
209 }
210 context.closePath();
211 if (buffer) return context = null, buffer + "" || null;
212 }
213 arc.centroid = function () {
214 var r = (+innerRadius.apply(this, arguments) + +outerRadius.apply(this, arguments)) / 2,
215 a = (+startAngle.apply(this, arguments) + +endAngle.apply(this, arguments)) / 2 - _math.pi / 2;
216 return [(0, _math.cos)(a) * r, (0, _math.sin)(a) * r];
217 };
218 arc.innerRadius = function (_) {
219 return arguments.length ? (innerRadius = typeof _ === "function" ? _ : (0, _constant.default)(+_), arc) : innerRadius;
220 };
221 arc.outerRadius = function (_) {
222 return arguments.length ? (outerRadius = typeof _ === "function" ? _ : (0, _constant.default)(+_), arc) : outerRadius;
223 };
224 arc.cornerRadius = function (_) {
225 return arguments.length ? (cornerRadius = typeof _ === "function" ? _ : (0, _constant.default)(+_), arc) : cornerRadius;
226 };
227 arc.padRadius = function (_) {
228 return arguments.length ? (padRadius = _ == null ? null : typeof _ === "function" ? _ : (0, _constant.default)(+_), arc) : padRadius;
229 };
230 arc.startAngle = function (_) {
231 return arguments.length ? (startAngle = typeof _ === "function" ? _ : (0, _constant.default)(+_), arc) : startAngle;
232 };
233 arc.endAngle = function (_) {
234 return arguments.length ? (endAngle = typeof _ === "function" ? _ : (0, _constant.default)(+_), arc) : endAngle;
235 };
236 arc.padAngle = function (_) {
237 return arguments.length ? (padAngle = typeof _ === "function" ? _ : (0, _constant.default)(+_), arc) : padAngle;
238 };
239 arc.context = function (_) {
240 return arguments.length ? (context = _ == null ? null : _, arc) : context;
241 };
242 return arc;
243}
Note: See TracBrowser for help on using the repository browser.