vis.js is a dynamic, browser-based visualization library
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

108 lines
2.9 KiB

  1. import EdgeBase from './EdgeBase'
  2. class BezierEdgeBase extends EdgeBase {
  3. constructor(options, body, labelModule) {
  4. super(options, body, labelModule);
  5. }
  6. /**
  7. * This function uses binary search to look for the point where the bezier curve crosses the border of the node.
  8. *
  9. * @param nearNode
  10. * @param ctx
  11. * @param viaNode
  12. * @param nearNode
  13. * @param ctx
  14. * @param viaNode
  15. * @param nearNode
  16. * @param ctx
  17. * @param viaNode
  18. */
  19. _findBorderPositionBezier(nearNode, ctx, viaNode = this._getViaCoordinates()) {
  20. var maxIterations = 10;
  21. var iteration = 0;
  22. var low = 0;
  23. var high = 1;
  24. var pos, angle, distanceToBorder, distanceToPoint, difference;
  25. var threshold = 0.2;
  26. var node = this.to;
  27. var from = false;
  28. if (nearNode.id === this.from.id) {
  29. node = this.from;
  30. from = true;
  31. }
  32. while (low <= high && iteration < maxIterations) {
  33. var middle = (low + high) * 0.5;
  34. pos = this.getPoint(middle, viaNode);
  35. angle = Math.atan2((node.y - pos.y), (node.x - pos.x));
  36. distanceToBorder = node.distanceToBorder(ctx, angle);
  37. distanceToPoint = Math.sqrt(Math.pow(pos.x - node.x, 2) + Math.pow(pos.y - node.y, 2));
  38. difference = distanceToBorder - distanceToPoint;
  39. if (Math.abs(difference) < threshold) {
  40. break; // found
  41. }
  42. else if (difference < 0) { // distance to nodes is larger than distance to border --> t needs to be bigger if we're looking at the to node.
  43. if (from === false) {
  44. low = middle;
  45. }
  46. else {
  47. high = middle;
  48. }
  49. }
  50. else {
  51. if (from === false) {
  52. high = middle;
  53. }
  54. else {
  55. low = middle;
  56. }
  57. }
  58. iteration++;
  59. }
  60. pos.t = middle;
  61. return pos;
  62. }
  63. /**
  64. * Calculate the distance between a point (x3,y3) and a line segment from
  65. * (x1,y1) to (x2,y2).
  66. * http://stackoverflow.com/questions/849211/shortest-distancae-between-a-point-and-a-line-segment
  67. * @param {number} x1
  68. * @param {number} y1
  69. * @param {number} x2
  70. * @param {number} y2
  71. * @param {number} x3
  72. * @param {number} y3
  73. * @private
  74. */
  75. _getDistanceToBezierEdge(x1, y1, x2, y2, x3, y3, via) { // x3,y3 is the point
  76. let xVia, yVia;
  77. xVia = via.x;
  78. yVia = via.y;
  79. let minDistance = 1e9;
  80. let distance;
  81. let i, t, x, y;
  82. let lastX = x1;
  83. let lastY = y1;
  84. for (i = 1; i < 10; i++) {
  85. t = 0.1 * i;
  86. x = Math.pow(1 - t, 2) * x1 + (2 * t * (1 - t)) * xVia + Math.pow(t, 2) * x2;
  87. y = Math.pow(1 - t, 2) * y1 + (2 * t * (1 - t)) * yVia + Math.pow(t, 2) * y2;
  88. if (i > 0) {
  89. distance = this._getDistanceToLine(lastX, lastY, x, y, x3, y3);
  90. minDistance = distance < minDistance ? distance : minDistance;
  91. }
  92. lastX = x;
  93. lastY = y;
  94. }
  95. return minDistance;
  96. }
  97. }
  98. export default BezierEdgeBase;