Yog.Layout.Grid (YogEx v0.99.1)

Copy Markdown View Source

Grid layout algorithm for positioning graph nodes in Elixir.

Positions nodes deterministically on a 2D grid based on user-supplied rows or columns. This layout is ideal for structured diagrams (e.g. architecture layouts, UMLs, C4 models) where predictability, alignment, and pixel-exact placement are required rather than organic force-directed aesthetics.

Mathematical Model

Given a 2D grid origin $(O_x, O_y)$ and cell dimensions $(W_{ ext{cell}}, H_{ ext{cell}})$:

Each node positioned at grid coordinates $( ext{row}, ext{col})$ is mapped to: $$ x = O_x + ext{col} cdot W_{ ext{cell}} $$ $$ y = O_y + ext{row} cdot H_{ ext{cell}} $$

Empty cells can be specified using a placeholder (nil or :_), which leaves the slot empty and aligns subsequent elements correctly.

Complexities

  • Time Complexity: $O(V)$ where $V$ is the number of nodes.
  • Space Complexity: $O(V)$ auxiliary space.

Summary

Functions

Positions nodes on a grid using rows or columns.

Functions

layout(graph, opts)

@spec layout(
  Yog.Graph.t(),
  keyword()
) :: %{required(Yog.Graph.node_id()) => {float(), float()}}

Positions nodes on a grid using rows or columns.

Options

  • :rows - List of lists of node IDs, representing rows of the grid.
  • :columns - List of lists of node IDs, representing columns of the grid.
  • :cell - A {cell_width, cell_height} tuple representing dimensions of each grid cell (default: {1.0, 1.0}).
  • :cell_width - Bypasses width from :cell if specified.
  • :cell_height - Bypasses height from :cell if specified.
  • :origin - A {x_origin, y_origin} tuple representing the top-left offset of the grid (default: {0.0, 0.0}).

Examples

iex> graph = Yog.undirected() |> Yog.add_nodes_from([:client, :api, :db])
iex> pos = Yog.Layout.Grid.layout(graph, rows: [[:client], [:api], [:db]], cell: {100, 50})
iex> pos[:client]
{0.0, 0.0}
iex> pos[:api]
{0.0, 50.0}