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.

112 lines
2.9 KiB

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