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.

105 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 from x
  68. * @param {number} y1 from y
  69. * @param {number} x2 to x
  70. * @param {number} y2 to y
  71. * @param {number} x3 point to check x
  72. * @param {number} y3 point to check y
  73. * @private
  74. */
  75. _getDistanceToBezierEdge(x1, y1, x2, y2, x3, y3, via) { // x3,y3 is the point
  76. let minDistance = 1e9;
  77. let distance;
  78. let i, t, x, y;
  79. let lastX = x1;
  80. let lastY = y1;
  81. for (i = 1; i < 10; i++) {
  82. t = 0.1 * i;
  83. x = Math.pow(1 - t, 2) * x1 + (2 * t * (1 - t)) * via.x + Math.pow(t, 2) * x2;
  84. y = Math.pow(1 - t, 2) * y1 + (2 * t * (1 - t)) * via.y + Math.pow(t, 2) * y2;
  85. if (i > 0) {
  86. distance = this._getDistanceToLine(lastX, lastY, x, y, x3, y3);
  87. minDistance = distance < minDistance ? distance : minDistance;
  88. }
  89. lastX = x;
  90. lastY = y;
  91. }
  92. return minDistance;
  93. }
  94. }
  95. export default BezierEdgeBase;