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.

31 lines
824 B

  1. /**
  2. * Created by Alex on 2/23/2015.
  3. */
  4. function CentralGravitySolver(body, options) {
  5. this.body = body;
  6. this.options = options;
  7. }
  8. CentralGravitySolver.prototype.solve = function () {
  9. var dx, dy, distance, node, i;
  10. var nodes = this.body.calculationNodes;
  11. var gravity = this.options.centralGravity;
  12. var gravityForce = 0;
  13. for (i = 0; i < this.body.calculationNodeIndices.length; i++) {
  14. node = nodes[this.body.calculationNodeIndices[i]];
  15. node.damping = this.options.damping; // possibly add function to alter damping properties of clusters.
  16. dx = -node.x;
  17. dy = -node.y;
  18. distance = Math.sqrt(dx * dx + dy * dy);
  19. gravityForce = (distance == 0) ? 0 : (gravity / distance);
  20. node.fx = dx * gravityForce;
  21. node.fy = dy * gravityForce;
  22. }
  23. };
  24. module.exports = CentralGravitySolver;