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.

69 lines
1.9 KiB

  1. /**
  2. * Created by Alex on 2/23/2015.
  3. */
  4. class RepulsionSolver {
  5. constructor(body, physicsBody, options) {
  6. this.body = body;
  7. this.physicsBody = physicsBody;
  8. this.options = options;
  9. }
  10. /**
  11. * Calculate the forces the nodes apply on each other based on a repulsion field.
  12. * This field is linearly approximated.
  13. *
  14. * @private
  15. */
  16. solve() {
  17. var dx, dy, distance, fx, fy, repulsingForce, node1, node2;
  18. var nodes = this.physicsBody.calculationNodes;
  19. var nodeIndices = this.physicsBody.calculationNodeIndices;
  20. // repulsing forces between nodes
  21. var nodeDistance = this.options.nodeDistance;
  22. // approximation constants
  23. var a = (-2 / 3) /nodeDistance;
  24. var b = 4 / 3;
  25. // we loop from i over all but the last entree in the array
  26. // j loops from i+1 to the last. This way we do not double count any of the indices, nor i == j
  27. for (let i = 0; i < nodeIndices.length - 1; i++) {
  28. node1 = nodes[nodeIndices[i]];
  29. for (let j = i + 1; j < nodeIndices.length; j++) {
  30. node2 = nodes[nodeIndices[j]];
  31. dx = node2.x - node1.x;
  32. dy = node2.y - node1.y;
  33. distance = Math.sqrt(dx * dx + dy * dy);
  34. // same condition as BarnesHutSolver, making sure nodes are never 100% overlapping.
  35. if (distance == 0) {
  36. distance = 0.1*Math.random();
  37. dx = distance;
  38. }
  39. if (distance < 2 * nodeDistance) {
  40. if (distance < 0.5 * nodeDistance) {
  41. repulsingForce = 1.0;
  42. }
  43. else {
  44. repulsingForce = a * distance + b; // linear approx of 1 / (1 + Math.exp((distance / nodeDistance - 1) * steepness))
  45. }
  46. repulsingForce = repulsingForce / distance;
  47. fx = dx * repulsingForce;
  48. fy = dy * repulsingForce;
  49. node1.fx -= fx;
  50. node1.fy -= fy;
  51. node2.fx += fx;
  52. node2.fy += fy;
  53. }
  54. }
  55. }
  56. }
  57. }
  58. export {RepulsionSolver};