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.

40 lines
1.3 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. if (this.overlapAvoidanceFactor < 1 && node.shape.radius) {
  22. distance = Math.max(0.1 + (this.overlapAvoidanceFactor * node.shape.radius), distance - node.shape.radius);
  23. }
  24. let degree = (node.edges.length + 1);
  25. // the dividing by the distance cubed instead of squared allows us to get the fx and fy components without sines and cosines
  26. // it is shorthand for gravityforce with distance squared and fx = dx/distance * gravityForce
  27. let gravityForce = this.options.gravitationalConstant * parentBranch.mass * node.options.mass * degree / Math.pow(distance,2);
  28. let fx = dx * gravityForce;
  29. let fy = dy * gravityForce;
  30. this.physicsBody.forces[node.id].x += fx;
  31. this.physicsBody.forces[node.id].y += fy;
  32. }
  33. }
  34. export default ForceAtlas2BasedRepulsionSolver;