Skip to content

Instantly share code, notes, and snippets.

@nazywamsiepawel
Created August 22, 2014 08:25
Show Gist options
  • Save nazywamsiepawel/c1b816b200005858e753 to your computer and use it in GitHub Desktop.
Save nazywamsiepawel/c1b816b200005858e753 to your computer and use it in GitHub Desktop.
Graph.prototype.forces = function(){
for(var i=0; i<this.nodesList.length; i++){
netForceX = 0;
netForceY = 0;
var dsq;
var a = this.nodesList[i];
/*calculate repulsion*/
for(var j=0; j<this.nodesList.length; j++){
var b = this.nodesList[j];
if(j!=i){
// net-force := net-force + Coulomb_repulsion( this_node, other_node )
netForceY+=0;//coloumbForY;
netForceX+=0;//coloumbForX;
dsq = (a.x - b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y);
if(dsq==0) dsq = 0.001;
var c = this.repulsion/dsq;
a.velX += c * (a.x-b.x);
a.velY += c * (a.y-b.y);
}
}
var neighbours = this.getNodeNeighbours(i);
for(var j=0; j<neighbours.length; j++){
// net-force := net-force + Hooke_attraction( this_node, spring )
var b = this.nodesList[neighbours[j]];
a.velX += this.attraction*(b.x - a.x);
a.velY += this.attraction*(b.y - a.y);
b.velX += this.attraction*(a.x - b.x);
b.velY += this.attraction*(a.y - b.y);
}
var dis = 0;
for(var j=0; j<this.nodesList.length; j++){
a = this.nodesList[i];
a.x = (a.x + a.velX/20)*0.9;
a.y = (a.y + a.velY/20)*0.9;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment