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.

34 lines
1.1 KiB

  1. import BarnesHutSolver from "./BarnesHutSolver"
  2. class ForceAtlas2BasedRepulsionSolver extends BarnesHutSolver {
  3. constructor(body, physicsBody, options) {
  4. super(body, physicsBody, options);
  5. }
  6. /**
  7. * Calculate the forces based on the distance.
  8. *
  9. * @param distance
  10. * @param dx
  11. * @param dy
  12. * @param node
  13. * @param parentBranch
  14. * @private
  15. */
  16. _calculateForces(distance, dx, dy, node, parentBranch) {
  17. if (distance === 0) {
  18. distance = 0.1 * Math.random();
  19. dx = distance;
  20. }
  21. let degree = (node.edges.length + 1);
  22. // the dividing by the distance cubed instead of squared allows us to get the fx and fy components without sines and cosines
  23. // it is shorthand for gravityforce with distance squared and fx = dx/distance * gravityForce
  24. let gravityForce = this.options.gravitationalConstant * parentBranch.mass * node.options.mass * degree / Math.pow(distance,2);
  25. let fx = dx * gravityForce;
  26. let fy = dy * gravityForce;
  27. this.physicsBody.forces[node.id].x += fx;
  28. this.physicsBody.forces[node.id].y += fy;
  29. }
  30. }
  31. export default ForceAtlas2BasedRepulsionSolver;