helix.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.helix = void 0;
  4. /* eslint-disable @typescript-eslint/no-unused-vars */
  5. var scale_1 = require("@antv/scale");
  6. var utils_1 = require("../utils");
  7. /**
  8. * Maps normalized value to normalized helix coordinate at the center of the bounding box.
  9. * @param params [x0, x1, y0, y1]
  10. * @param x x of the the bounding box of coordinate
  11. * @param y y of the the bounding box of coordinate
  12. * @param width width of the the bounding box of coordinate
  13. * @param height height of the the bounding box of coordinate
  14. * @returns transformer
  15. */
  16. var helix = function (params, x, y, width, height) {
  17. var _a = params, startAngle = _a[0], endAngle = _a[1], innerRadius = _a[2], outerRadius = _a[3];
  18. // 计算螺旋系数:r = a + b * theta
  19. // d = 2 * PI * b
  20. // 这里不管 startAngle 从多少开始,都从 0 开始计算
  21. // 这样才能保证坐标系在 bounding box 里面
  22. var count = (endAngle - 0) / (2 * Math.PI) + 1;
  23. var d = (outerRadius - innerRadius) / count;
  24. var b = d / (Math.PI * 2);
  25. // 当 theta 为 0 的时候的极径
  26. var step = new scale_1.Linear({
  27. range: [innerRadius, innerRadius + d * 0.99], // 防止和下一个螺线重合
  28. });
  29. var angle = new scale_1.Linear({
  30. range: [startAngle, endAngle],
  31. });
  32. var aspect = height / width;
  33. var sx = aspect > 1 ? 1 : aspect;
  34. var sy = aspect > 1 ? 1 / aspect : 1;
  35. return {
  36. transform: function (vector) {
  37. var v1 = vector[0], v2 = vector[1];
  38. var theta = angle.map(v1);
  39. var a = step.map(v2);
  40. // 根据长宽比调整,使得极坐标系内切外接矩形
  41. var x = Math.cos(theta) * (b * theta + a) * sx;
  42. var y = Math.sin(theta) * (b * theta + a) * sy;
  43. // 将坐标的原点移动到外接矩形的中心,并且将长度设置为一半
  44. var dx = x * 0.5 + 0.5;
  45. var dy = y * 0.5 + 0.5;
  46. return [dx, dy];
  47. },
  48. untransform: function (vector) {
  49. var dx = vector[0], dy = vector[1];
  50. var x = ((dx - 0.5) * 2) / sx;
  51. var y = ((dy - 0.5) * 2) / sy;
  52. var r = Math.sqrt(Math.pow(x, 2) + Math.pow(y, 2));
  53. var t = Math.atan2(y, x) + Math.floor(r / d) * Math.PI * 2;
  54. var theta = (0, utils_1.adjustAngle)(t, startAngle, endAngle);
  55. var a = r - b * theta;
  56. var v1 = angle.invert(theta);
  57. var v2 = step.invert(a);
  58. return [v1, v2];
  59. },
  60. };
  61. };
  62. exports.helix = helix;
  63. //# sourceMappingURL=helix.js.map