f08562fe105ac7346cb66b19d25def561dacc4b498fefb0a19bb3b4107d3eef07db78796b7dfeaf0c6b4dbf093f0e8873fa8a27bee275ca86cf424105beab1 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /**
  2. * 内外旋轮曲线
  3. * @module zrender/graphic/shape/Trochold
  4. */
  5. import Path, { PathProps } from '../Path';
  6. const cos = Math.cos;
  7. const sin = Math.sin;
  8. export class TrochoidShape {
  9. cx = 0
  10. cy = 0
  11. r = 0
  12. r0 = 0
  13. d = 0
  14. location = 'out'
  15. }
  16. export interface TrochoidProps extends PathProps {
  17. shape?: Partial<TrochoidShape>
  18. }
  19. class Trochoid extends Path<TrochoidProps> {
  20. shape: TrochoidShape
  21. constructor(opts?: TrochoidProps) {
  22. super(opts);
  23. }
  24. getDefaultStyle() {
  25. return {
  26. stroke: '#000',
  27. fill: null as string
  28. };
  29. }
  30. getDefaultShape() {
  31. return new TrochoidShape();
  32. }
  33. buildPath(ctx: CanvasRenderingContext2D, shape: TrochoidShape) {
  34. const R = shape.r;
  35. const r = shape.r0;
  36. const d = shape.d;
  37. const offsetX = shape.cx;
  38. const offsetY = shape.cy;
  39. const delta = shape.location === 'out' ? 1 : -1;
  40. let x1;
  41. let y1;
  42. let x2;
  43. let y2;
  44. if (shape.location && R <= r) {
  45. return;
  46. }
  47. let num = 0;
  48. let i = 1;
  49. let theta;
  50. x1 = (R + delta * r) * cos(0)
  51. - delta * d * cos(0) + offsetX;
  52. y1 = (R + delta * r) * sin(0)
  53. - d * sin(0) + offsetY;
  54. ctx.moveTo(x1, y1);
  55. // 计算结束时的i
  56. do {
  57. num++;
  58. }
  59. while ((r * num) % (R + delta * r) !== 0);
  60. do {
  61. theta = Math.PI / 180 * i;
  62. x2 = (R + delta * r) * cos(theta)
  63. - delta * d * cos((R / r + delta) * theta)
  64. + offsetX;
  65. y2 = (R + delta * r) * sin(theta)
  66. - d * sin((R / r + delta) * theta)
  67. + offsetY;
  68. ctx.lineTo(x2, y2);
  69. i++;
  70. }
  71. while (i <= (r * num) / (R + delta * r) * 360);
  72. }
  73. }
  74. Trochoid.prototype.type = 'trochoid';
  75. export default Trochoid;