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.

114 lines
2.9 KiB

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