Created
May 1, 2011 21:59
-
-
Save abernier/950917 to your computer and use it in GitHub Desktop.
Force-based algorithms (graph drawing) — http://en.wikipedia.org/wiki/Force-based_algorithms_(graph_drawing)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Pseudocode | |
Each node has x,y position and dx,dy velocity and mass m. There is usually a spring constant, s, and damping: 0 < damping < 1. The force toward and away from nodes is calculated according to Hooke's Law and Coulomb's law or similar as discussed above. | |
set up initial node velocities to (0,0) | |
set up initial node positions randomly // make sure no 2 nodes are in exactly the same position | |
loop | |
total_kinetic_energy := 0 // running sum of total kinetic energy over all particles | |
for each node | |
net-force := (0, 0) // running sum of total force on this particular node | |
for each other node | |
net-force := net-force + Coulomb_repulsion( this_node, other_node ) | |
next node | |
for each spring connected to this node | |
net-force := net-force + Hooke_attraction( this_node, spring ) | |
next spring | |
// without damping, it moves forever | |
this_node.velocity := (this_node.velocity + timestep * net-force) * damping | |
this_node.position := this_node.position + timestep * this_node.velocity | |
total_kinetic_energy := total_kinetic_energy + this_node.mass * (this_node.velocity)^2 | |
next node | |
until total_kinetic_energy is less than some small number // the simulation has stopped moving |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment