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