Deterministic force-directed layouts on indexed nodes.
moon add gaato/force///|
test "lay out a path of three nodes" {
let sim = @force.Simulation::new(3)
sim.add_force(@force.link([(0, 1), (1, 2)]))
sim.add_force(@force.many_body())
sim.add_force(@force.center())
while sim.alpha > sim.alpha_min {
sim.tick()
}
// Draw node i at (sim.xs()[i], sim.ys()[i]).
let ends = (sim.xs()[0] - sim.xs()[2], sim.ys()[0] - sim.ys()[2])
assert_true(ends.0 * ends.0 + ends.1 * ends.1 > 0)
}| Constructor | What it does | d3 equivalent |
|---|---|---|
| link(edges) | Pulls connected nodes toward a distance | forceLink |
| many_body() | Repels (or attracts) every pair, with Barnes–Hut approximation | forceManyBody |
| center() | Keeps the layout's centre of mass in place | forceCenter |
| collide() | Keeps nodes from overlapping | forceCollide |
| x(), y() | Pulls nodes toward a coordinate on one axis | forceX, forceY |
///|
test "per-node settings" {
let sim = @force.Simulation::new(3)
sim.add_force(@force.many_body(strength=All(-30)))
sim.add_force(@force.collide(radius=Each([4, 8, 12])))
sim.tick(iterations=10)
}type Forcepub struct Simulation {
alpha : Double
alpha_min : Double
alpha_decay : Double
alpha_target : Double
velocity_decay : Double
// private fields
}test {
let sim = @force.Simulation::new(3)
sim.add_force(@force.link([(0, 1), (1, 2)]))
sim.add_force(@force.many_body())
sim.tick(iterations=300)
inspect(sim.node_count(), content="3")
}test {
let _ = @force.many_body(strength=All(-30))
let _ = @force.x(target=Each([0, 100, 200]))
}