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
761 B

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