# `Yog.Builder.GridGraph`
[🔗](https://github.com/code-shoily/yog_ex/blob/v0.97.0/lib/yog/builder/grid_graph.ex#L1)

Grid graph builder result.

A grid graph is a structured graph where nodes are arranged in a 2D grid
with edges connecting adjacent cells according to a specified topology.

## Fields

- `graph` - The underlying Yog graph
- `rows` - Number of rows in the grid
- `cols` - Number of columns in the grid
- `topology` - Connection pattern (`:rook`, `:queen`, `:king`, etc.)
- `predicate` - Optional function to filter valid cells

## Topologies

- `:rook` - 4-connected (up, down, left, right)
- `:queen` - 8-connected (rook + diagonals)
- `:king` - Same as queen
- `:bishop` - Diagonal connections only

## Examples

    iex> graph = Yog.undirected() |> Yog.add_node(0, "data")
    iex> grid = %Yog.Builder.GridGraph{
    ...>   graph: graph,
    ...>   rows: 3,
    ...>   cols: 3,
    ...>   topology: :rook
    ...> }
    iex> grid.rows
    3
    iex> Yog.Builder.GridGraph.coord_to_id(grid, 0, 0)
    0

# `t`

```elixir
@type t() :: %Yog.Builder.GridGraph{
  cols: non_neg_integer(),
  graph: Yog.graph(),
  predicate: (non_neg_integer(), non_neg_integer() -&gt; boolean()) | nil,
  rows: non_neg_integer(),
  topology: atom()
}
```

# `coord_to_id`

```elixir
@spec coord_to_id(t(), non_neg_integer(), non_neg_integer()) :: Yog.Model.node_id()
```

Converts grid coordinates to a node ID.

The default mapping is: `row * cols + col`

## Examples

    iex> graph = Yog.undirected()
    iex> grid = %Yog.Builder.GridGraph{graph: graph, rows: 3, cols: 4}
    iex> Yog.Builder.GridGraph.coord_to_id(grid, 0, 0)
    0
    iex> Yog.Builder.GridGraph.coord_to_id(grid, 1, 2)
    6
    iex> Yog.Builder.GridGraph.coord_to_id(grid, 2, 3)
    11

# `from_map`

```elixir
@spec from_map(map()) :: t()
```

Backward compatibility: convert from legacy map format.

# `get_cell`

```elixir
@spec get_cell(t(), non_neg_integer(), non_neg_integer()) ::
  {:ok, term()} | {:error, nil}
```

Gets the cell data at a specific grid coordinate.

Returns `{:ok, data}` if the cell exists, or `{:error, nil}` otherwise.

## Examples

    iex> graph = Yog.undirected() |> Yog.add_node(4, "some_data")
    iex> grid = %Yog.Builder.GridGraph{graph: graph, rows: 3, cols: 3}
    iex> Yog.Builder.GridGraph.get_cell(grid, 1, 1)
    {:ok, "some_data"}
    iex> Yog.Builder.GridGraph.get_cell(grid, 10, 10)
    {:error, nil}

# `id_to_coord`

```elixir
@spec id_to_coord(t(), Yog.Model.node_id()) :: {non_neg_integer(), non_neg_integer()}
```

Converts a node ID back to grid coordinates.

Returns `{row, col}`.

## Examples

    iex> graph = Yog.undirected()
    iex> grid = %Yog.Builder.GridGraph{graph: graph, rows: 3, cols: 4}
    iex> Yog.Builder.GridGraph.id_to_coord(grid, 0)
    {0, 0}
    iex> Yog.Builder.GridGraph.id_to_coord(grid, 6)
    {1, 2}
    iex> Yog.Builder.GridGraph.id_to_coord(grid, 11)
    {2, 3}

# `new`

```elixir
@spec new(Yog.graph(), non_neg_integer(), non_neg_integer()) :: t()
```

Creates a new grid graph result.

## Examples

    iex> graph = Yog.undirected()
    iex> grid = Yog.Builder.GridGraph.new(graph, 3, 4)
    iex> grid.rows
    3
    iex> grid.cols
    4
    iex> grid.topology
    :rook

# `new`

```elixir
@spec new(Yog.graph(), non_neg_integer(), non_neg_integer(), atom()) :: t()
```

Creates a new grid graph result with topology.

## Examples

    iex> graph = Yog.undirected()
    iex> grid = Yog.Builder.GridGraph.new(graph, 3, 4, :queen)
    iex> grid.topology
    :queen

# `to_graph`

```elixir
@spec to_graph(t()) :: Yog.graph()
```

Unwraps the grid graph to return the plain graph.

## Examples

    iex> graph = Yog.undirected()
    iex> grid = %Yog.Builder.GridGraph{graph: graph, rows: 2, cols: 2}
    iex> Yog.Builder.GridGraph.to_graph(grid) == graph
    true

# `to_map`

```elixir
@spec to_map(t()) :: map()
```

Convert to legacy map format.

# `valid_coord?`

```elixir
@spec valid_coord?(t(), non_neg_integer(), non_neg_integer()) :: boolean()
```

Checks if a coordinate is within the grid bounds.

## Examples

    iex> graph = Yog.undirected()
    iex> grid = %Yog.Builder.GridGraph{graph: graph, rows: 3, cols: 4}
    iex> Yog.Builder.GridGraph.valid_coord?(grid, 1, 2)
    true
    iex> Yog.Builder.GridGraph.valid_coord?(grid, 5, 5)
    false

---

*Consult [api-reference.md](api-reference.md) for complete listing*
