Liking cljdoc? Tell your friends :D

fastmath.grid

Grid calculation functions.

Convert 2d coordinates into various grid coordinates and back.

Terms used:

  • cell type - grid cell shape: square, triangular, hexagonal, rhomboidal
  • coords - 2d euclidean coordinates (x,y)
  • cell - cell coordinates (q,r)
  • anchor - cell position in 2d euclidean space
  • corners - shape vertices
  • size - size of the cell.

Grids

Each grid is defined by cell type and size. Optionally you can provide translating vector.

Each cell has it's own coordinates, mostly axial based (only square has offset).

size means the distance between neighboring cell anchors (equivalently, for :square/:rhombus/:triangle, the cell's own side length; :shifted-square keeps its own q-direction/side-length convention, see below). For hexagonal cells, grid's size argument is scaled internally to a circumradius of size/√3 (so that adjacent hex centers are size apart); for the rest of the cell types size is the length of the side, used as passed.

Cell types are:

  • :square
  • :shifted-square
  • :triangle
  • :rhombus
  • :flat-hex - flat topped
  • :pointy-hex - pointy topped

Notes

  • Hexagonal grids are based on https://www.redblobgames.com/grids/hexagons/
  • Only hexagonal cells have anchor at the center. For the rest the anchor is at the top left vertex.
    • Anchors for triangular grids are shared between two cells: even and odd q coordinate. Even q is pointy topped, odd q is flat topped which is marked in the third coordinate of anchor.
Grid calculation functions.

Convert 2d coordinates into various grid coordinates and back.

Terms used:

* cell type - grid cell shape: square, triangular, hexagonal, rhomboidal
* coords - 2d euclidean coordinates (x,y)
* cell - cell coordinates (q,r)
* anchor - cell position in 2d euclidean space
* corners - shape vertices
* size - size of the cell.

### Grids

Each grid is defined by cell type and size. Optionally you can provide translating vector.

Each cell has it's own coordinates, mostly axial based (only square has offset).

`size` means the distance between neighboring cell anchors (equivalently, for `:square`/`:rhombus`/`:triangle`, the cell's own side length; `:shifted-square` keeps its own q-direction/side-length convention, see below). For hexagonal cells, [[grid]]'s `size` argument is scaled internally to a circumradius of `size/√3` (so that adjacent hex centers are `size` apart); for the rest of the cell types `size` is the length of the side, used as passed.

Cell types are:

* `:square`
* `:shifted-square`
* `:triangle`
* `:rhombus`
* `:flat-hex` - flat topped
* `:pointy-hex` - pointy topped

### Notes

* Hexagonal grids are based on https://www.redblobgames.com/grids/hexagons/
* Only hexagonal cells have anchor at the center. For the rest the anchor is at the top left vertex.
    * Anchors for triangular grids are shared between two cells: even and odd `q` coordinate. Even `q` is pointy topped, odd `q` is flat topped which is marked in the third coordinate of anchor.
raw docstring

areaclj

(area g)

Returns the area of a single cell of grid g.

Parameters:

  • g: a grid object created by grid.

Returns a double. Every cell of a given grid is congruent (same type and size), so this is a grid-level constant, not dependent on coords/cell, unlike corners.

See also area->size.

Returns the area of a single cell of grid `g`.

Parameters:

- `g`: a grid object created by [[grid]].

Returns a `double`. Every cell of a given grid is congruent (same type and `size`), so this is a grid-level constant, not dependent on `coords`/`cell`, unlike [[corners]].

See also [[area->size]].
raw docstring

area->sizeclj

(area->size type area)

Returns the size for which a type cell has the given area.

Parameters:

  • type (keyword): one of cell-names.
  • area (double): target cell area.

Returns a double: the size to pass to grid so that (area (grid type size)) equals the given area (inverse of area).

Throws ExceptionInfo when type is not one of cell-names.

See also area, grid.

Returns the `size` for which a `type` cell has the given `area`.

Parameters:

- `type` (keyword): one of [[cell-names]].
- `area` (double): target cell area.

Returns a `double`: the `size` to pass to [[grid]] so that `(area (grid type size))` equals the given `area` (inverse of [[area]]).

Throws `ExceptionInfo` when `type` is not one of [[cell-names]].

See also [[area]], [[grid]].
raw docstring

cell->anchorclj

(cell->anchor g cell)
(cell->anchor g q r)

Converts cell coordinates (q,r) to anchor coordinates (x,y).

Parameters:

  • g: a grid object created by grid.
  • cell: [q r] pair, or pass q/r separately.

Returns the cell's anchor point. For :pointy-hex/:flat-hex the anchor is the cell's center; for all other types it is the top left vertex (see the namespace docstring). For :triangle, the returned anchor is a 3d vector whose third coordinate is 0 for an up-pointing cell and 1 for a down-pointing one; up/down pairs sharing the same (q,r) base share the same first two coordinates.

Converts cell coordinates `(q,r)` to anchor coordinates `(x,y)`.

Parameters:

- `g`: a grid object created by [[grid]].
- `cell`: `[q r]` pair, or pass `q`/`r` separately.

Returns the cell's anchor point. For `:pointy-hex`/`:flat-hex` the anchor is the cell's center; for all other types it is the top left vertex (see the namespace docstring). For `:triangle`, the returned anchor is a 3d vector whose third coordinate is `0` for an up-pointing cell and `1` for a down-pointing one; up/down pairs sharing the same `(q,r)` base share the same first two coordinates.
raw docstring

cell->midclj

(cell->mid g cell)
(cell->mid g x y)

Converts cell coordinates (q,r) to the cell's midpoint (x,y).

Parameters:

  • g: a grid object created by grid.
  • cell: [q r] pair, or pass q/r separately.

Returns the centroid of the given cell. For :pointy-hex/:flat-hex this equals cell->anchor (the anchor is already the center).

Converts cell coordinates `(q,r)` to the cell's midpoint `(x,y)`.

Parameters:

- `g`: a grid object created by [[grid]].
- `cell`: `[q r]` pair, or pass `q`/`r` separately.

Returns the centroid of the given cell. For `:pointy-hex`/`:flat-hex` this equals [[cell->anchor]] (the anchor is already the center).
raw docstring

cell-namesclj

List of valid grid cell types accepted by grid.

Returns a vector of 6 keywords: :square, :shifted-square, :triangle, :rhombus, :flat-hex, :pointy-hex.

List of valid grid cell types accepted by [[grid]].

Returns a vector of 6 keywords: `:square`, `:shifted-square`, `:triangle`, `:rhombus`, `:flat-hex`, `:pointy-hex`.
raw docstring

coords->anchorclj

(coords->anchor g coords)
(coords->anchor g x y)

Converts 2d coordinates (x,y) to the anchor of the cell containing them.

Parameters:

  • g: a grid object created by grid.
  • coords: [x y] pair, or pass x/y separately.

Equivalent to (cell->anchor g (coords->cell g coords)) — routes through the containing cell, unlike cell->anchor which takes cell coordinates (q,r) directly.

Converts 2d coordinates `(x,y)` to the anchor of the cell containing them.

Parameters:

- `g`: a grid object created by [[grid]].
- `coords`: `[x y]` pair, or pass `x`/`y` separately.

Equivalent to `(cell->anchor g (coords->cell g coords))` — routes through the containing cell, unlike [[cell->anchor]] which takes cell coordinates `(q,r)` directly.
raw docstring

coords->cellclj

(coords->cell g coords)
(coords->cell g x y)

Converts 2d space coordinates (x,y) to cell coordinates (q,r).

Parameters:

  • g: a grid object created by grid.
  • coords: [x y] pair, or pass x/y separately.

Returns a 2d vector (q,r) identifying the cell containing (x,y). Points on a cell boundary are attributed to exactly one adjoining cell; the inclusive/exclusive convention is specific to each cell type.

Converts 2d space coordinates `(x,y)` to cell coordinates `(q,r)`.

Parameters:

- `g`: a grid object created by [[grid]].
- `coords`: `[x y]` pair, or pass `x`/`y` separately.

Returns a 2d vector `(q,r)` identifying the cell containing `(x,y)`. Points on a cell boundary are attributed to exactly one adjoining cell; the inclusive/exclusive convention is specific to each cell type.
raw docstring

coords->midclj

(coords->mid g coords)
(coords->mid g x y)

Converts 2d space coordinates (x,y) into the midpoint of the cell containing them.

Parameters:

  • g: a grid object created by grid.
  • coords: [x y] pair, or pass x/y separately.

Returns the centroid of the cell containing (x,y), always strictly inside the cell's corners. Equivalent to (cell->mid g (coords->cell g coords)).

Converts 2d space coordinates `(x,y)` into the midpoint of the cell containing them.

Parameters:

- `g`: a grid object created by [[grid]].
- `coords`: `[x y]` pair, or pass `x`/`y` separately.

Returns the centroid of the cell containing `(x,y)`, always strictly inside the cell's [[corners]]. Equivalent to `(cell->mid g (coords->cell g coords))`.
raw docstring

cornersclj

(corners g coords)
(corners g coords scale)
(corners g x y scale)

Returns the vertices of the cell containing given 2d space coordinates.

Parameters:

  • g: a grid object created by grid.
  • coords: [x y] pair, or pass x/y separately.
  • scale (double, optional, default 1.0): scales the cell size used to compute vertices, without moving the cell's anchor. Values <1.0 shrink the polygon toward the anchor, >1.0 grow it.

Returns a sequence of 2d vertices, in a fixed winding order: 4 for :square/:shifted-square/:rhombus, 3 for :triangle, 6 for :flat-hex/:pointy-hex.

Returns the vertices of the cell containing given 2d space coordinates.

Parameters:

- `g`: a grid object created by [[grid]].
- `coords`: `[x y]` pair, or pass `x`/`y` separately.
- `scale` (double, optional, default `1.0`): scales the cell size used to compute vertices, without moving the cell's anchor. Values `<1.0` shrink the polygon toward the anchor, `>1.0` grow it.

Returns a sequence of 2d vertices, in a fixed winding order: 4 for `:square`/`:shifted-square`/`:rhombus`, 3 for `:triangle`, 6 for `:flat-hex`/`:pointy-hex`.
raw docstring

flat-hex-cornersclj

Function which returns vertices for flat topped hexagon for given size and coordinates.

Function which returns vertices for flat topped hexagon for given size and coordinates.
raw docstring

gridclj

(grid)
(grid type)
(grid type size)
(grid type size sv)
(grid type size svx svy)

Create a grid object for a given cell type, size and optional translating vector.

Parameters:

  • type (keyword): one of cell-names (:square, :shifted-square, :triangle, :rhombus, :flat-hex, :pointy-hex).
  • size (double, optional, default 10.0): distance between neighboring cell anchors (for :square/:rhombus/:triangle this equals the cell's own side length; :shifted-square keeps its own q-direction/side-length convention). For :pointy-hex/:flat-hex the value actually stored and used internally is the circumradius size/√3 (so adjacent hex centers end up size apart, matching the other cell types), not size itself.
  • sv (2-element vector/sequence, e.g. [sx sy], or a Vec2, optional, default [0.0 0.0]) or separate svx, svy: translation vector applied to every anchor/mid/corner coordinate.

Returns an object satisfying fastmath.protocols/GridProto (and Named/Object), usable with coords->cell, cell->anchor, coords->mid, cell->mid, corners, coords->anchor and grid-type.

Throws ExceptionInfo when type is not one of cell-names.

Defaults: (grid type size) translates by [0.0 0.0]; (grid type) also sets size to 10.0; (grid) creates (grid :square).

Create a grid object for a given cell `type`, `size` and optional translating vector.

Parameters:

- `type` (keyword): one of [[cell-names]] (`:square`, `:shifted-square`, `:triangle`, `:rhombus`, `:flat-hex`, `:pointy-hex`).
- `size` (double, optional, default `10.0`): distance between neighboring cell anchors (for `:square`/`:rhombus`/`:triangle` this equals the cell's own side length; `:shifted-square` keeps its own q-direction/side-length convention). For `:pointy-hex`/`:flat-hex` the value actually stored and used internally is the circumradius `size/√3` (so adjacent hex centers end up `size` apart, matching the other cell types), not `size` itself.
- `sv` (2-element vector/sequence, e.g. `[sx sy]`, or a `Vec2`, optional, default `[0.0 0.0]`) or separate `svx`, `svy`: translation vector applied to every anchor/mid/corner coordinate.

Returns an object satisfying [[fastmath.protocols/GridProto]] (and `Named`/`Object`), usable with [[coords->cell]], [[cell->anchor]], [[coords->mid]], [[cell->mid]], [[corners]], [[coords->anchor]] and [[grid-type]].

Throws `ExceptionInfo` when `type` is not one of [[cell-names]].

Defaults: `(grid type size)` translates by `[0.0 0.0]`; `(grid type)` also sets `size` to `10.0`; `(grid)` creates `(grid :square)`.
raw docstring

grid-typeclj

(grid-type g)

Returns the cell type of grid g.

Parameters:

  • g: a grid object created by grid.

Returns one of cell-names.

Returns the cell type of grid `g`.

Parameters:

- `g`: a grid object created by [[grid]].

Returns one of [[cell-names]].
raw docstring

pointy-hex-cornersclj

Function which returns vertices for pointy topped hexagon for given size and coordinates.

Function which returns vertices for pointy topped hexagon for given size and coordinates.
raw docstring

cljdoc builds & hosts documentation for Clojure/Script libraries

Keyboard shortcuts
Ctrl+kJump to recent docs
←Move to previous article
→Move to next article
Ctrl+/Jump to the search field
× close