get-arc-params.ts 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. const mod = function (n: number, m: number): number {
  2. return ((n % m) + m) % m;
  3. };
  4. // 向量长度
  5. function vMag(v) {
  6. return Math.sqrt(v[0] * v[0] + v[1] * v[1]);
  7. }
  8. // u.v/|u||v|,计算夹角的余弦值
  9. function vRatio(u, v) {
  10. // 当存在一个向量的长度为 0 时,夹角也为 0,即夹角的余弦值为 1
  11. return vMag(u) * vMag(v) ? (u[0] * v[0] + u[1] * v[1]) / (vMag(u) * vMag(v)) : 1;
  12. }
  13. // 向量角度
  14. function vAngle(u, v) {
  15. return (u[0] * v[1] < u[1] * v[0] ? -1 : 1) * Math.acos(vRatio(u, v));
  16. }
  17. /**
  18. * 判断两个点是否重合,点坐标的格式为 [x, y]
  19. * @param {Array} point1 第一个点
  20. * @param {Array} point2 第二个点
  21. */
  22. export function isSamePoint(point1, point2) {
  23. return point1[0] === point2[0] && point1[1] === point2[1];
  24. }
  25. // A 0:rx 1:ry 2:x-axis-rotation 3:large-arc-flag 4:sweep-flag 5: x 6: y
  26. export default function getArcParams(startPoint, params) {
  27. let rx = params[1];
  28. let ry = params[2];
  29. const xRotation = mod((params[3] * Math.PI) / 180, Math.PI * 2);
  30. const arcFlag = params[4];
  31. const sweepFlag = params[5];
  32. // 弧形起点坐标
  33. const x1 = startPoint[0];
  34. const y1 = startPoint[1];
  35. // 弧形终点坐标
  36. const x2 = params[6];
  37. const y2 = params[7];
  38. const xp = (Math.cos(xRotation) * (x1 - x2)) / 2.0 + (Math.sin(xRotation) * (y1 - y2)) / 2.0;
  39. const yp = (-1 * Math.sin(xRotation) * (x1 - x2)) / 2.0 + (Math.cos(xRotation) * (y1 - y2)) / 2.0;
  40. const lambda = (xp * xp) / (rx * rx) + (yp * yp) / (ry * ry);
  41. if (lambda > 1) {
  42. rx *= Math.sqrt(lambda);
  43. ry *= Math.sqrt(lambda);
  44. }
  45. const diff = rx * rx * (yp * yp) + ry * ry * (xp * xp);
  46. let f = diff ? Math.sqrt((rx * rx * (ry * ry) - diff) / diff) : 1;
  47. if (arcFlag === sweepFlag) {
  48. f *= -1;
  49. }
  50. if (isNaN(f)) {
  51. f = 0;
  52. }
  53. // 旋转前的起点坐标,且当长半轴和短半轴的长度为 0 时,坐标按 (0, 0) 处理
  54. const cxp = ry ? (f * rx * yp) / ry : 0;
  55. const cyp = rx ? (f * -ry * xp) / rx : 0;
  56. // 椭圆圆心坐标
  57. const cx = (x1 + x2) / 2.0 + Math.cos(xRotation) * cxp - Math.sin(xRotation) * cyp;
  58. const cy = (y1 + y2) / 2.0 + Math.sin(xRotation) * cxp + Math.cos(xRotation) * cyp;
  59. // 起始点的单位向量
  60. const u = [(xp - cxp) / rx, (yp - cyp) / ry];
  61. // 终止点的单位向量
  62. const v = [(-1 * xp - cxp) / rx, (-1 * yp - cyp) / ry];
  63. // 计算起始点和圆心的连线,与 x 轴正方向的夹角
  64. const theta = vAngle([1, 0], u);
  65. // 计算圆弧起始点和终止点与椭圆圆心连线的夹角
  66. let dTheta = vAngle(u, v);
  67. if (vRatio(u, v) <= -1) {
  68. dTheta = Math.PI;
  69. }
  70. if (vRatio(u, v) >= 1) {
  71. dTheta = 0;
  72. }
  73. if (sweepFlag === 0 && dTheta > 0) {
  74. dTheta = dTheta - 2 * Math.PI;
  75. }
  76. if (sweepFlag === 1 && dTheta < 0) {
  77. dTheta = dTheta + 2 * Math.PI;
  78. }
  79. return {
  80. cx,
  81. cy,
  82. // 弧形的起点和终点相同时,长轴和短轴的长度按 0 处理
  83. rx: isSamePoint(startPoint, [x2, y2]) ? 0 : rx,
  84. ry: isSamePoint(startPoint, [x2, y2]) ? 0 : ry,
  85. startAngle: theta,
  86. endAngle: theta + dTheta,
  87. xRotation,
  88. arcFlag,
  89. sweepFlag,
  90. };
  91. }