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).

For hexagonal cell size is a radius from midpoint to corner. For the rest it is the size of the side.

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).

For hexagonal cell size is a radius from midpoint to corner. For the rest it is the size of the side.

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

cell->anchorclj

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

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

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

cell->midclj

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

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

Converts cell coordinates (q,r) to mid point (x,y).
sourceraw docstring

cell-namesclj

source

coords->anchorclj

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

Converts 2d coordinates (x,y) to cell anchor (x,y).

Converts 2d coordinates (x,y) to cell anchor (x,y).
sourceraw docstring

coords->cellclj

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

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

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

coords->midclj

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

Converts 2d space coordinates (x,y) into cell midpoint (x,y).

Converts 2d space coordinates (x,y) into cell midpoint (x,y).
sourceraw docstring

cornersclj

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

Returns list of cell vertices for given 2d space coordinates.

Returns list of cell vertices for given 2d space coordinates.
sourceraw 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.
sourceraw docstring

gridclj

(grid)
(grid type)
(grid type size)
(grid type size sx sy)

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

Create grid for given type, size and optional translating vector.
sourceraw docstring

grid-typeclj

(grid-type g)

Returns type of the cell.

Returns type of the cell.
sourceraw 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.
sourceraw docstring

cljdoc is a website building & hosting documentation for Clojure/Script libraries

× close