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.

100 lines
2.6 KiB

  1. /**
  2. * Created by Alex on 2/23/2015.
  3. */
  4. function SpringSolver(body, options) {
  5. this.body = body;
  6. this.options = options;
  7. }
  8. /**
  9. * this function calculates the effects of the springs in the case of unsmooth curves.
  10. *
  11. * @private
  12. */
  13. SpringSolver.prototype._calculateSpringForces = function () {
  14. var edgeLength, edge, edgeId;
  15. var edges = this.edges;
  16. // forces caused by the edges, modelled as springs
  17. for (edgeId in edges) {
  18. if (edges.hasOwnProperty(edgeId)) {
  19. edge = edges[edgeId];
  20. if (edge.connected === true) {
  21. // only calculate forces if nodes are in the same sector
  22. if (this.nodes.hasOwnProperty(edge.toId) && this.nodes.hasOwnProperty(edge.fromId)) {
  23. edgeLength = edge.physics.springLength;
  24. this._calculateSpringForce(edge.from, edge.to, edgeLength);
  25. }
  26. }
  27. }
  28. }
  29. };
  30. /**
  31. * This function calculates the springforces on the nodes, accounting for the support nodes.
  32. *
  33. * @private
  34. */
  35. SpringSolver.prototype._calculateSpringForcesWithSupport = function () {
  36. var edgeLength, edge, edgeId;
  37. var edges = this.edges;
  38. // forces caused by the edges, modelled as springs
  39. for (edgeId in edges) {
  40. if (edges.hasOwnProperty(edgeId)) {
  41. edge = edges[edgeId];
  42. if (edge.connected === true) {
  43. // only calculate forces if nodes are in the same sector
  44. if (this.nodes.hasOwnProperty(edge.toId) && this.nodes.hasOwnProperty(edge.fromId)) {
  45. if (edge.via != null) {
  46. var node1 = edge.to;
  47. var node2 = edge.via;
  48. var node3 = edge.from;
  49. edgeLength = edge.physics.springLength;
  50. this._calculateSpringForce(node1, node2, 0.5 * edgeLength);
  51. this._calculateSpringForce(node2, node3, 0.5 * edgeLength);
  52. }
  53. }
  54. }
  55. }
  56. }
  57. };
  58. /**
  59. * This is the code actually performing the calculation for the function above. It is split out to avoid repetition.
  60. *
  61. * @param node1
  62. * @param node2
  63. * @param edgeLength
  64. * @private
  65. */
  66. SpringSolver.prototype._calculateSpringForce = function (node1, node2, edgeLength) {
  67. var dx, dy, fx, fy, springForce, distance;
  68. dx = (node1.x - node2.x);
  69. dy = (node1.y - node2.y);
  70. distance = Math.sqrt(dx * dx + dy * dy);
  71. distance = distance == 0 ? 0.01 : distance;
  72. // the 1/distance is so the fx and fy can be calculated without sine or cosine.
  73. springForce = this.options.springConstant * (edgeLength - distance) / distance;
  74. fx = dx * springForce;
  75. fy = dy * springForce;
  76. node1.fx += fx;
  77. node1.fy += fy;
  78. node2.fx -= fx;
  79. node2.fy -= fy;
  80. };
  81. module.exports = SpringSolver;