Circle.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. import {RedBlackNode} from "./RedBlackTree";
  2. import {circles, epsilon2} from "./Diagram";
  3. var circlePool = [];
  4. export var firstCircle;
  5. function Circle() {
  6. RedBlackNode(this);
  7. this.x =
  8. this.y =
  9. this.arc =
  10. this.site =
  11. this.cy = null;
  12. }
  13. export function attachCircle(arc) {
  14. var lArc = arc.P,
  15. rArc = arc.N;
  16. if (!lArc || !rArc) return;
  17. var lSite = lArc.site,
  18. cSite = arc.site,
  19. rSite = rArc.site;
  20. if (lSite === rSite) return;
  21. var bx = cSite[0],
  22. by = cSite[1],
  23. ax = lSite[0] - bx,
  24. ay = lSite[1] - by,
  25. cx = rSite[0] - bx,
  26. cy = rSite[1] - by;
  27. var d = 2 * (ax * cy - ay * cx);
  28. if (d >= -epsilon2) return;
  29. var ha = ax * ax + ay * ay,
  30. hc = cx * cx + cy * cy,
  31. x = (cy * ha - ay * hc) / d,
  32. y = (ax * hc - cx * ha) / d;
  33. var circle = circlePool.pop() || new Circle;
  34. circle.arc = arc;
  35. circle.site = cSite;
  36. circle.x = x + bx;
  37. circle.y = (circle.cy = y + by) + Math.sqrt(x * x + y * y); // y bottom
  38. arc.circle = circle;
  39. var before = null,
  40. node = circles._;
  41. while (node) {
  42. if (circle.y < node.y || (circle.y === node.y && circle.x <= node.x)) {
  43. if (node.L) node = node.L;
  44. else { before = node.P; break; }
  45. } else {
  46. if (node.R) node = node.R;
  47. else { before = node; break; }
  48. }
  49. }
  50. circles.insert(before, circle);
  51. if (!before) firstCircle = circle;
  52. }
  53. export function detachCircle(arc) {
  54. var circle = arc.circle;
  55. if (circle) {
  56. if (!circle.P) firstCircle = circle.N;
  57. circles.remove(circle);
  58. circlePool.push(circle);
  59. RedBlackNode(circle);
  60. arc.circle = null;
  61. }
  62. }