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.

65 lines
2.1 KiB

  1. /**
  2. * Created by Alex on 2/10/14.
  3. */
  4. var repulsionMixin = {
  5. /**
  6. * Calculate the forces the nodes apply on eachother based on a repulsion field.
  7. * This field is linearly approximated.
  8. *
  9. * @private
  10. */
  11. _calculateNodeForces: function () {
  12. var dx, dy, angle, distance, fx, fy, combinedClusterSize,
  13. repulsingForce, node1, node2, i, j;
  14. var nodes = this.calculationNodes;
  15. var nodeIndices = this.calculationNodeIndices;
  16. // approximation constants
  17. var a_base = -2 / 3;
  18. var b = 4 / 3;
  19. // repulsing forces between nodes
  20. var nodeDistance = this.constants.physics.repulsion.nodeDistance;
  21. var minimumDistance = nodeDistance;
  22. // we loop from i over all but the last entree in the array
  23. // j loops from i+1 to the last. This way we do not double count any of the indices, nor i == j
  24. for (i = 0; i < nodeIndices.length - 1; i++) {
  25. node1 = nodes[nodeIndices[i]];
  26. for (j = i + 1; j < nodeIndices.length; j++) {
  27. node2 = nodes[nodeIndices[j]];
  28. combinedClusterSize = node1.clusterSize + node2.clusterSize - 2;
  29. dx = node2.x - node1.x;
  30. dy = node2.y - node1.y;
  31. distance = Math.sqrt(dx * dx + dy * dy);
  32. minimumDistance = (combinedClusterSize == 0) ? nodeDistance : (nodeDistance * (1 + combinedClusterSize * this.constants.clustering.distanceAmplification));
  33. var a = a_base / minimumDistance;
  34. if (distance < 2 * minimumDistance) {
  35. if (distance < 0.5 * minimumDistance) {
  36. repulsingForce = 1.0;
  37. }
  38. else {
  39. repulsingForce = a * distance + b; // linear approx of 1 / (1 + Math.exp((distance / minimumDistance - 1) * steepness))
  40. }
  41. // amplify the repulsion for clusters.
  42. repulsingForce *= (combinedClusterSize == 0) ? 1 : 1 + combinedClusterSize * this.constants.clustering.forceAmplification;
  43. repulsingForce = repulsingForce / distance;
  44. fx = dx * repulsingForce;
  45. fy = dy * repulsingForce;
  46. node1.fx -= fx;
  47. node1.fy -= fy;
  48. node2.fx += fx;
  49. node2.fy += fy;
  50. }
  51. }
  52. }
  53. }
  54. };