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.

50 lines
1.6 KiB

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