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.

47 lines
1.5 KiB

  1. import BezierEdgeBase from './BezierEdgeBase'
  2. class CubicBezierEdgeBase extends BezierEdgeBase {
  3. constructor(id, options, body, labelModule) {
  4. super(id, 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. * @private
  18. */
  19. _getDistanceToBezierEdge(x1, y1, x2, y2, x3, y3, via1, via2) { // x3,y3 is the point
  20. let minDistance = 1e9;
  21. let distance;
  22. let i, t, x, y;
  23. let lastX = x1;
  24. let lastY = y1;
  25. let vec = [0,0,0,0]
  26. for (i = 1; i < 10; i++) {
  27. t = 0.1 * i;
  28. vec[0] = Math.pow(1 - t, 3);
  29. vec[1] = 3 * t * Math.pow(1 - t, 2);
  30. vec[2] = 3 * Math.pow(t,2) * (1 - t);
  31. vec[3] = Math.pow(t, 3);
  32. x = vec[0] * x1 + vec[1] * via1.x + vec[2] * via2.x + vec[3] * x2;
  33. y = vec[0] * y1 + vec[1] * via1.y + vec[2] * via2.y + vec[3] * y2;
  34. if (i > 0) {
  35. distance = this._getDistanceToLine(lastX, lastY, x, y, x3, y3);
  36. minDistance = distance < minDistance ? distance : minDistance;
  37. }
  38. lastX = x;
  39. lastY = y;
  40. }
  41. return minDistance;
  42. }
  43. }
  44. export default CubicBezierEdgeBase;