The force simulation as one pure value, stepped by whoever holds it (spec/08 §9).
iex> alias Macaw.Draw.Layout.{Force, Graph}
iex> {:ok, graph} = Graph.new(["a"], ["b"], nil, nil, 1)
iex> {:ok, sim} = Force.new(nodes: graph)
iex> sim = Force.step(sim, 3)
iex> {Force.running?(sim), length(Force.nodes(sim))}
{true, 2}visualize ran it synchronously (Force.run/1) or as a GenServer on a 16 ms timer. Here
it is %Force{}: no process, timer or subscriber. Under Macaw the Frame's contained
value holds it and macaw_live's contained cell drives it.
- Nodes carry
id,x,y,vx,vy,fx,fy; a node is placed on the phyllotaxis spiral (radius = sqrt(0.5 + i) · 10,angle = i · π · (3 − sqrt 5)) unless it is given a position, and a non-nil fx(fy) pins its x (y) (§9.1). - A tick sets
alpha = alpha + (alpha_target − alpha) · alpha_decay, applies the forces in order (Macaw.Draw.Layout.Force.Forces), then integrates every free coordinate:v = v · velocity_decay; p = p + v. Nothing reads a clock or randomness, so equal options give equal values (§9.3). - Warm start (
warm/3) continues a changed graph from the last layout, andrescale/3maps a layout to a new plot size without reheating it (§9.4, §9.5).
This is visualize's Visualize.Layout.Force and the tick of its Force.Simulation, with
the warm start of Visualize.Chart.Transform.Force; the GenServer, its timer, tagged
ticks and subscribers are not carried over (spec/08 §17).
Summary
Functions
The columns of the simulation (§9.3): :nodes, "row", "id", "x", "y" in node
order; :links, "row", "source", "target" (the ids), "x0", "y0", "x1",
"y1", "path" (the straight line) in link order.
A simulation (§9.3). Options, every one checked and every fault answered at once
The nodes with their x, y, vx, vy, in node order (§9.3).
Pins the node id at {x, y} (§9.3): {:ok, sim}, or {:error, {:unknown_node, id}}.
alpha set, clamped into [0, 1]: d3's alpha(a).restart() (§9.3).
The simulation rescaled from a plot of {w, h} to one of {w2, h2} (§9.5): every x
and vx (and pin) by w2 / w, every y and vy by h2 / h, the center force moved to
the new plot's center, alpha unchanged. A zero extent keeps its axis.
Ticks until alpha < alpha_min before a tick, or max_ticks ticks (§9.3): visualize's
run/1, a cold layout from the spiral at alpha 1 with every option at its default.
Whether a holder that steps the simulation would see it move (§9.3): alpha ≥ alpha_min,
or alpha_target ≥ alpha_min.
n ticks of the simulation (§9.3); step(sim, 0) is sim.
alpha_target set, clamped into [0, 1] (§9.3).
The milliseconds to the simulation's next step while it is running?/1, else nil
(§9.4): the pace is the value's, the timer its holder's.
Unpins the node id (§9.3): {:ok, sim}, or {:error, {:unknown_node, id}}.
The next layout of a graph whose data changed, from the last one (§9.4). opts are
new/1's but nodes and links, with seeds:, a map from id to {x, y} (a nodes
slot's numeric x and y), and alpha: (0.3).
Types
@type force() :: {Macaw.Draw.Layout.Force.Forces.type(), map()}
A force and its checked options.
@type sim_node() :: %{ :id => term(), :x => number(), :y => number(), :vx => number(), :vy => number(), :fx => number() | nil, :fy => number() | nil, optional(atom()) => term() }
A simulated node.
@type t() :: %Macaw.Draw.Layout.Force{ alpha: number(), alpha_decay: number(), alpha_min: number(), alpha_target: number(), forces: [force()], index: %{required(term()) => non_neg_integer()}, link_rows: [non_neg_integer()], links: [Macaw.Draw.Layout.Force.Forces.link()], node_rows: [non_neg_integer()] | nil, nodes: [sim_node()], velocity_decay: number() }
Functions
@spec columns(t(), atom() | String.t()) :: %{required(String.t()) => [term()]} | Macaw.Draw.Layout.error()
The columns of the simulation (§9.3): :nodes, "row", "id", "x", "y" in node
order; :links, "row", "source", "target" (the ids), "x0", "y0", "x1",
"y1", "path" (the straight line) in link order.
@spec new() :: {:ok, t()}
new/1 with every option at its default: a simulation of no nodes.
@spec new(keyword() | map()) :: {:ok, t()} | Macaw.Draw.Layout.error()
A simulation (§9.3). Options, every one checked and every fault answered at once:
| Option | Type | Default |
|---|---|---|
nodes | a Graph, or a list of node maps each with id | [] |
links | a list of {source, target} node positions, or maps with source, target and optional distance, strength; nil takes a graph's | nil |
forces | a list of {type, opts} (Forces) | center, many-body −30, link 30 |
alpha | number, clamped into [0, 1] | 1.0 |
alpha_min | non-negative number | 0.001 |
alpha_decay | number in [0, 1] | 1 − alpha_min^(1/300) |
alpha_target | number, clamped into [0, 1] | 0 |
velocity_decay | number in [0, 1], the share of velocity kept per tick | 0.4 |
@spec nodes(t()) :: [sim_node()] | Macaw.Draw.Layout.error()
The nodes with their x, y, vx, vy, in node order (§9.3).
@spec pin(t(), term(), number(), number()) :: {:ok, t()} | {:error, {:unknown_node, term()}} | Macaw.Draw.Layout.error()
Pins the node id at {x, y} (§9.3): {:ok, sim}, or {:error, {:unknown_node, id}}.
@spec reheat(t(), number()) :: t() | Macaw.Draw.Layout.error()
alpha set, clamped into [0, 1]: d3's alpha(a).restart() (§9.3).
The simulation rescaled from a plot of {w, h} to one of {w2, h2} (§9.5): every x
and vx (and pin) by w2 / w, every y and vy by h2 / h, the center force moved to
the new plot's center, alpha unchanged. A zero extent keeps its axis.
@spec run(t()) :: t() | Macaw.Draw.Layout.error()
run/2 for at most 300 ticks.
@spec run(t(), non_neg_integer()) :: t() | Macaw.Draw.Layout.error()
Ticks until alpha < alpha_min before a tick, or max_ticks ticks (§9.3): visualize's
run/1, a cold layout from the spiral at alpha 1 with every option at its default.
@spec running?(t()) :: boolean() | Macaw.Draw.Layout.error()
Whether a holder that steps the simulation would see it move (§9.3): alpha ≥ alpha_min,
or alpha_target ≥ alpha_min.
@spec step(t(), non_neg_integer()) :: t() | Macaw.Draw.Layout.error()
n ticks of the simulation (§9.3); step(sim, 0) is sim.
@spec target(t(), number()) :: t() | Macaw.Draw.Layout.error()
alpha_target set, clamped into [0, 1] (§9.3).
@spec tick_interval(t()) :: pos_integer() | nil | Macaw.Draw.Layout.error()
The milliseconds to the simulation's next step while it is running?/1, else nil
(§9.4): the pace is the value's, the timer its holder's.
@spec unpin(t(), term()) :: {:ok, t()} | {:error, {:unknown_node, term()}} | Macaw.Draw.Layout.error()
Unpins the node id (§9.3): {:ok, sim}, or {:error, {:unknown_node, id}}.
@spec warm(t(), Macaw.Draw.Layout.Graph.t(), keyword()) :: {:ok, t()} | Macaw.Draw.Layout.error()
The next layout of a graph whose data changed, from the last one (§9.4). opts are
new/1's but nodes and links, with seeds:, a map from id to {x, y} (a nodes
slot's numeric x and y), and alpha: (0.3).
A node previous knows starts where it was, moving as it was; an unknown seeded node at
its seed; any other new node at the mean position of its laid-out linked neighbors, or
of every laid-out node when it has none, offset by its spiral position. A node that has
gone is forgotten. The simulation is reheated to alpha with d3's own decay. A graph
none of whose nodes previous knows is cold, new/1's at alpha 1.