Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I wrote some code awhile ago to perform dynamic graph layouts using C# and XNA (full source available upon request).</p> <p>Here are some of the critical functions:</p> <pre><code> public void UpdateNodes() { for (int i = 0; i &lt; nodes.Count; i++) { Vector2 netForce = Vector2.Zero; foreach (Node otherNode in nodes) { if (otherNode != nodes[i]) { netForce += CoulombRepulsion(nodes[i], otherNode); //calculate repulsion for all nodes if (nodes[i].links.Contains(otherNode)) { netForce += HookeAttraction(nodes[i], otherNode); //only calc attraction for linked nodes } } } nodes[i].Velocity += netForce; nodes[i].Velocity *= .99f; nodes[i].Position += nodes[i].Velocity; } } public Vector2 HookeAttraction(Node node1, Node node2) //ON node1 BY node2 { Vector2 direction = Vector2.Subtract(node2.Position, node1.Position); direction.Normalize(); return hookeConst* node2.Mass * Vector2.Distance(node1.Position, node2.Position) * direction; } public Vector2 GravAttraction(Node node1, Node node2) //ON node1 BY node2 { Vector2 direction = Vector2.Subtract(node2.Position, node1.Position); direction.Normalize(); return gravConst * node2.Mass * Vector2.DistanceSquared(node1.Position, node2.Position) * direction; } </code></pre> <p>Pick the two constants based on how fast you want the graph to converge. I used these:</p> <pre><code> private const float hookeConst = .000005f; private const float gravConst = .00000001f; </code></pre> <p>That code is pretty self-explanatory, but feel free to ask if you need anything. Basically, call the UpdateNodes() function in a loop, and your graph will converge on its minimal-energy state.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload