Browse Source

removed duplicate code

flowchartTest
Alex de Mulder 9 years ago
parent
commit
06ce63eb42
1 changed files with 27 additions and 22 deletions
  1. +27
    -22
      lib/network/modules/components/physics/BarnesHutSolver.js

+ 27
- 22
lib/network/modules/components/physics/BarnesHutSolver.js View File

@ -72,17 +72,7 @@ class BarnesHutSolver {
// original condition : s/d < theta = passed === d/s > 1/theta = passed // original condition : s/d < theta = passed === d/s > 1/theta = passed
// calcSize = 1/s --> d * 1/s > 1/theta = passed // calcSize = 1/s --> d * 1/s > 1/theta = passed
if (distance * parentBranch.calcSize > this.thetaInversed) { if (distance * parentBranch.calcSize > this.thetaInversed) {
// duplicate code to reduce function calls to speed up program
if (distance === 0) {
distance = 0.1 * Math.random();
dx = distance;
}
var gravityForce = this.options.gravitationalConstant * parentBranch.mass * node.options.mass / (distance * distance * distance);
var fx = dx * gravityForce;
var fy = dy * gravityForce;
this.physicsBody.forces[node.id].x += fx;
this.physicsBody.forces[node.id].y += fy;
this._calculateForces(distance, dx, dy, node, parentBranch);
} }
else { else {
// Did not pass the condition, go into children if available // Did not pass the condition, go into children if available
@ -94,17 +84,7 @@ class BarnesHutSolver {
} }
else { // parentBranch must have only one node, if it was empty we wouldnt be here else { // parentBranch must have only one node, if it was empty we wouldnt be here
if (parentBranch.children.data.id != node.id) { // if it is not self if (parentBranch.children.data.id != node.id) { // if it is not self
// duplicate code to reduce function calls to speed up program
if (distance === 0) {
distance = 0.5 * Math.random();
dx = distance;
}
var gravityForce = this.options.gravitationalConstant * parentBranch.mass * node.options.mass / (distance * distance * distance);
var fx = dx * gravityForce;
var fy = dy * gravityForce;
this.physicsBody.forces[node.id].x += fx;
this.physicsBody.forces[node.id].y += fy;
this._calculateForces(distance, dx, dy, node, parentBranch);
} }
} }
} }
@ -112,6 +92,31 @@ class BarnesHutSolver {
} }
/**
* Calculate the forces based on the distance.
*
* @param distance
* @param dx
* @param dy
* @param node
* @param parentBranch
* @private
*/
_calculateForces(distance, dx, dy, node, parentBranch) {
// duplicate code to reduce function calls to speed up program
if (distance === 0) {
distance = 0.1 * Math.random();
dx = distance;
}
var gravityForce = this.options.gravitationalConstant * parentBranch.mass * node.options.mass / (distance * distance * distance);
var fx = dx * gravityForce;
var fy = dy * gravityForce;
this.physicsBody.forces[node.id].x += fx;
this.physicsBody.forces[node.id].y += fy;
}
/** /**
* This function constructs the barnesHut tree recursively. It creates the root, splits it and starts placing the nodes. * This function constructs the barnesHut tree recursively. It creates the root, splits it and starts placing the nodes.
* *

Loading…
Cancel
Save