1 line
6.0 KiB
Plaintext
1 line
6.0 KiB
Plaintext
{"version":3,"file":"ArcUtils.mjs","sources":["../../src/utils/ArcUtils.ts"],"sourcesContent":["import { PI_2 } from '@pixi/core';\nimport { curves } from '../const';\n\ninterface IArcLikeShape\n{\n cx: number;\n cy: number;\n radius: number;\n startAngle: number;\n endAngle: number;\n anticlockwise: boolean;\n}\n\n/**\n * Utilities for arc curves.\n * @private\n */\nexport class ArcUtils\n{\n /**\n * Calculate information of the arc for {@link PIXI.Graphics.arcTo}.\n * @private\n * @param x1 - The x-coordinate of the first control point of the arc\n * @param y1 - The y-coordinate of the first control point of the arc\n * @param x2 - The x-coordinate of the second control point of the arc\n * @param y2 - The y-coordinate of the second control point of the arc\n * @param radius - The radius of the arc\n * @param points - Collection of points to add to\n * @returns - If the arc length is valid, return center of circle, radius and other info otherwise `null`.\n */\n static curveTo(x1: number, y1: number, x2: number, y2: number, radius: number, points: Array<number>): IArcLikeShape\n {\n const fromX = points[points.length - 2];\n const fromY = points[points.length - 1];\n\n const a1 = fromY - y1;\n const b1 = fromX - x1;\n const a2 = y2 - y1;\n const b2 = x2 - x1;\n const mm = Math.abs((a1 * b2) - (b1 * a2));\n\n if (mm < 1.0e-8 || radius === 0)\n {\n if (points[points.length - 2] !== x1 || points[points.length - 1] !== y1)\n {\n points.push(x1, y1);\n }\n\n return null;\n }\n\n const dd = (a1 * a1) + (b1 * b1);\n const cc = (a2 * a2) + (b2 * b2);\n const tt = (a1 * a2) + (b1 * b2);\n const k1 = radius * Math.sqrt(dd) / mm;\n const k2 = radius * Math.sqrt(cc) / mm;\n const j1 = k1 * tt / dd;\n const j2 = k2 * tt / cc;\n const cx = (k1 * b2) + (k2 * b1);\n const cy = (k1 * a2) + (k2 * a1);\n const px = b1 * (k2 + j1);\n const py = a1 * (k2 + j1);\n const qx = b2 * (k1 + j2);\n const qy = a2 * (k1 + j2);\n const startAngle = Math.atan2(py - cy, px - cx);\n const endAngle = Math.atan2(qy - cy, qx - cx);\n\n return {\n cx: (cx + x1),\n cy: (cy + y1),\n radius,\n startAngle,\n endAngle,\n anticlockwise: (b1 * a2 > b2 * a1),\n };\n }\n\n /**\n * The arc method creates an arc/curve (used to create circles, or parts of circles).\n * @private\n * @param _startX - Start x location of arc\n * @param _startY - Start y location of arc\n * @param cx - The x-coordinate of the center of the circle\n * @param cy - The y-coordinate of the center of the circle\n * @param radius - The radius of the circle\n * @param startAngle - The starting angle, in radians (0 is at the 3 o'clock position\n * of the arc's circle)\n * @param endAngle - The ending angle, in radians\n * @param _anticlockwise - Specifies whether the drawing should be\n * counter-clockwise or clockwise. False is default, and indicates clockwise, while true\n * indicates counter-clockwise.\n * @param points - Collection of points to add to\n */\n static arc(_startX: number, _startY: number, cx: number, cy: number, radius: number,\n startAngle: number, endAngle: number, _anticlockwise: boolean, points: Array<number>): void\n {\n const sweep = endAngle - startAngle;\n const n = curves._segmentsCount(\n Math.abs(sweep) * radius,\n Math.ceil(Math.abs(sweep) / PI_2) * 40\n );\n\n const theta = (sweep) / (n * 2);\n const theta2 = theta * 2;\n const cTheta = Math.cos(theta);\n const sTheta = Math.sin(theta);\n const segMinus = n - 1;\n const remainder = (segMinus % 1) / segMinus;\n\n for (let i = 0; i <= segMinus; ++i)\n {\n const real = i + (remainder * i);\n const angle = ((theta) + startAngle + (theta2 * real));\n const c = Math.cos(angle);\n const s = -Math.sin(angle);\n\n points.push(\n (((cTheta * c) + (sTheta * s)) * radius) + cx,\n (((cTheta * -s) + (sTheta * c)) * radius) + cy\n );\n }\n }\n}\n"],"names":[],"mappings":";;AAiBO,MAAM,SACb;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAYI,OAAO,QAAQ,IAAY,IAAY,IAAY,IAAY,QAAgB,QAC/E;AACI,UAAM,QAAQ,OAAO,OAAO,SAAS,CAAC,GAGhC,KAFQ,OAAO,OAAO,SAAS,CAAC,IAEnB,IACb,KAAK,QAAQ,IACb,KAAK,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