diagram.js 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. import { intersectionArea } from './circleintersection';
  2. /**
  3. * 根据圆心(x, y) 半径 r 返回圆的绘制 path
  4. * @param x 圆心点 x
  5. * @param y 圆心点 y
  6. * @param r 圆的半径
  7. * @returns 圆的 path
  8. */
  9. function circlePath(x, y, r) {
  10. const ret = [];
  11. // ret.push('\nM', x, y);
  12. // ret.push('\nm', -r, 0);
  13. // ret.push('\na', r, r, 0, 1, 0, r * 2, 0);
  14. // ret.push('\na', r, r, 0, 1, 0, -r * 2, 0);
  15. const x0 = x - r;
  16. const y0 = y;
  17. ret.push('M', x0, y0);
  18. ret.push('A', r, r, 0, 1, 0, x0 + 2 * r, y0);
  19. ret.push('A', r, r, 0, 1, 0, x0, y0);
  20. return ret.join(' ');
  21. }
  22. /** returns a svg path of the intersection area of a bunch of circles */
  23. export function intersectionAreaPath(circles) {
  24. const stats = {};
  25. intersectionArea(circles, stats);
  26. const arcs = stats.arcs;
  27. if (arcs.length === 0) {
  28. return 'M 0 0';
  29. }
  30. else if (arcs.length == 1) {
  31. const circle = arcs[0].circle;
  32. return circlePath(circle.x, circle.y, circle.radius);
  33. }
  34. else {
  35. // draw path around arcs
  36. const ret = ['\nM', arcs[0].p2.x, arcs[0].p2.y];
  37. for (let i = 0; i < arcs.length; ++i) {
  38. const arc = arcs[i], r = arc.circle.radius, wide = arc.width > r;
  39. ret.push('\nA', r, r, 0, wide ? 1 : 0, 1, arc.p1.x, arc.p1.y);
  40. }
  41. return ret.join(' ');
  42. }
  43. }
  44. //# sourceMappingURL=diagram.js.map