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.

63 lines
1.9 KiB

  1. import BezierEdgeBase from './BezierEdgeBase'
  2. /**
  3. * A Base Class for all Cubic Bezier Edges. Bezier curves are used to model
  4. * smooth gradual curves in paths between nodes.
  5. *
  6. * @class CubicBezierEdgeBase
  7. * @extends BezierEdgeBase
  8. */
  9. class CubicBezierEdgeBase extends BezierEdgeBase {
  10. /**
  11. * @param {Object} options
  12. * @param {Object} body
  13. * @param {Label} labelModule
  14. * @constructor CubicBezierEdgeBase
  15. */
  16. constructor(options, body, labelModule) {
  17. super(options, body, labelModule);
  18. }
  19. /**
  20. * Calculate the distance between a point (x3,y3) and a line segment from
  21. * (x1,y1) to (x2,y2).
  22. * http://stackoverflow.com/questions/849211/shortest-distancae-between-a-point-and-a-line-segment
  23. * https://en.wikipedia.org/wiki/B%C3%A9zier_curve
  24. * @param {number} x1 from x
  25. * @param {number} y1 from y
  26. * @param {number} x2 to x
  27. * @param {number} y2 to y
  28. * @param {number} x3 point to check x
  29. * @param {number} y3 point to check y
  30. * @param {vis.Node} via1
  31. * @param {vis.Node} via2
  32. * @returns {number}
  33. * @private
  34. */
  35. _getDistanceToBezierEdge(x1, y1, x2, y2, x3, y3, via1, via2) { // x3,y3 is the point
  36. let minDistance = 1e9;
  37. let distance;
  38. let i, t, x, y;
  39. let lastX = x1;
  40. let lastY = y1;
  41. let vec = [0,0,0,0]
  42. for (i = 1; i < 10; i++) {
  43. t = 0.1 * i;
  44. vec[0] = Math.pow(1 - t, 3);
  45. vec[1] = 3 * t * Math.pow(1 - t, 2);
  46. vec[2] = 3 * Math.pow(t,2) * (1 - t);
  47. vec[3] = Math.pow(t, 3);
  48. x = vec[0] * x1 + vec[1] * via1.x + vec[2] * via2.x + vec[3] * x2;
  49. y = vec[0] * y1 + vec[1] * via1.y + vec[2] * via2.y + vec[3] * y2;
  50. if (i > 0) {
  51. distance = this._getDistanceToLine(lastX, lastY, x, y, x3, y3);
  52. minDistance = distance < minDistance ? distance : minDistance;
  53. }
  54. lastX = x;
  55. lastY = y;
  56. }
  57. return minDistance;
  58. }
  59. }
  60. export default CubicBezierEdgeBase;