Macaw.Draw.Layout.Pack (macaw_draw v0.1.0)

Copy Markdown View Source

Circle packing (spec/08 §4): every node a circle, leaves sized by value and internal nodes enclosing their children.

iex> alias Macaw.Draw.Layout.{Hierarchy, Pack}
iex> {:ok, root, []} = Hierarchy.stratify(~w(r a b), [nil, "r", "r"], [])
iex> {:ok, pack} = Pack.new(size: {100, 100})
iex> laid = Pack.generate(pack, Hierarchy.count(root))
iex> {laid.x, laid.y, laid.r}
{50.0, 50.0, 50.0}

Options (§4.1), checked by new/1:

OptionTypeDefault
size{w, h} of non-negative numbers{1, 1}
paddingnon-negative number, or function of an internal node answering one0
radiusfunction of a leaf answering a number, or nilnil

generate/2 expects value set by sum/2 or count/1 unless radius is given; a leaf radius below 0 is clamped to 0. Siblings are packed by d3-hierarchy's packSiblings, in child order, against the front chain; the enclosing circle is Welzl's over a shuffle driven by d3's linear congruential generator from seed 1, so the result is fixed. Padding is d3's two passes; the root is scaled into the box and centered in it. This is visualize's Visualize.Layout.Pack, its setters replaced by new/1.

Summary

Types

A circle: center and radius.

t()

Functions

The columns of a packed hierarchy (§3.5): :nodes with "x", "y", "r", or :links, each link the straight line between the centers whatever its shape:.

The minimal circle enclosing circles, each {x, y, r} (§4.2): Welzl's algorithm over a Fisher–Yates shuffle driven by d3's linear congruential generator (a = 1664525, c = 1013904223, m = 2^32, seed 1), so the answer is fixed. {0, 0, 0} for []; a list holding a term that is no circle is {[], {:type, :circles}}.

Every node of root placed as a circle, x, y and r (§4.2). A root of radius 0 or nil is answered without scaling and with leaf positions unset.

new/1 with every option at its default.

The layout of opts (§4.1): {:ok, pack} or {:error, faults}.

Types

circle()

@type circle() :: {number(), number(), number()}

A circle: center and radius.

t()

@type t() :: %Macaw.Draw.Layout.Pack{
  padding: number() | (Macaw.Draw.Layout.Hierarchy.t() -> number()),
  radius: (Macaw.Draw.Layout.Hierarchy.t() -> number()) | nil,
  size: {number(), number()}
}

Functions

columns(root, output, opts)

@spec columns(Macaw.Draw.Layout.Hierarchy.t(), atom() | String.t(), keyword()) ::
  %{required(String.t()) => [term()]} | Macaw.Draw.Layout.error()

The columns of a packed hierarchy (§3.5): :nodes with "x", "y", "r", or :links, each link the straight line between the centers whatever its shape:.

enclose(circles)

@spec enclose([circle()]) :: circle() | Macaw.Draw.Layout.error()

The minimal circle enclosing circles, each {x, y, r} (§4.2): Welzl's algorithm over a Fisher–Yates shuffle driven by d3's linear congruential generator (a = 1664525, c = 1013904223, m = 2^32, seed 1), so the answer is fixed. {0, 0, 0} for []; a list holding a term that is no circle is {[], {:type, :circles}}.

iex> Macaw.Draw.Layout.Pack.enclose([{0, 0, 1}, {4, 0, 1}])
{2.0, 0.0, 3.0}

generate(pack, root)

Every node of root placed as a circle, x, y and r (§4.2). A root of radius 0 or nil is answered without scaling and with leaf positions unset.

new()

@spec new() :: {:ok, t()}

new/1 with every option at its default.

new(opts)

@spec new(keyword() | map()) :: {:ok, t()} | Macaw.Draw.Layout.error()

The layout of opts (§4.1): {:ok, pack} or {:error, faults}.