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.

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