sqrid
sqrid provides square grid coordinates and related operations, in a single-file create, with no dependencies.
It's easier to explain the features of this crate in terms of the types it provides:
Qa: position, as absolute coordinates in a grid of fixed size. The dimensions of the grid are const generics type parameters; invalid coordinates can't be created.Qr: "movement", relative coordinates. These are the cardinal (and intercardinal) directions. Addition is implemented in the form ofQa + Qr = Option<Qa>, which can beNoneif the result is outside the grid.Grid: aQa-indexed array.
All these types have the standard iter, iter_mut, extend,
as_ref, and conversion operations that should be expected.
Qa: absolute coordinates, position
The Qa type represents an absolute position in a square
grid. The type itself receives the height and width of the grid as
const generic parameter.
We should usually create a type alias for the grid size we are using:
use sqrid;
type Qa = Qa;
We can get Qa instances by:
- Using one of the const associated items:
type Qa = Qa; const MY_FIRST : Qa = FIRST; const MY_LAST : Qa = LAST; - Using
try_fromwith a(i16, i16)tuple or a tuple reference:use TryFrom; use Error; type Qa = Qa; - Calling
Qa::new, which checks the bounds in const contexts:
The following, for instance, doesn't compile:type Qa = Qa; const MY_FIRST : Qa = ;type Qa = Qa; const MY_FIRST : Qa = ; - Calling
Qa::iterto iterate all coordinates in the grid:type Qa = Qa; for qa in iter
Qr: relative coordinates, direction, movement
This type represents a relative movement of one square. It can only be one of the 8 cardinal and intercardinal directions (N, NE, E, SE, S, SW, W, NW).
It's a building block for paths, iterating on a Qa neighbors,
etc. It effectively represents the edges in a graph where the
Qa type represents nodes.
We can get Qr instances by:
- Using one of the const associated items that represent all
cardinal directions (recommended):
use Qr; const RIGHT : Qr = E; const DOWN : Qr = S; - Using
try_fromwith a(i16, i16)tuple or a tuple reference:use TryFrom; use Error; use Qr; - Calling
Qr::iterto iterate all directions:
The const argument to Qr::iter signals it to iterate over the intercardinal directions too. Passingfor qr infalsegets us only the 4 cardinal directions.
Grid: a Qa-indexed array
A grid is a generic array that can be indexed by a Qa
We can create the type from a suitable Qa type by using the
grid_create macro. We can then interact with specific lines
with Grid::line and Grid::line_mut, or with the whole
underlying array with as_ref and
as_mut.
Usage example:
type Qa = Qa;
type Grid = grid_create!;
// The grid create macro above is currently equivalent to:
type Grid2 = Grid WIDTH }, ,
>;
// We can create grids from iterators via `collect`:
let mut gridnums = .;
// Iterate on their members:
for i in &gridnums
// Change the members in a loop:
for i in &mut gridnums
// Iterate on (coordinate, member) tuples:
for in gridnums.iter_qa
// And we can always use `as_ref` or `as_mut` to interact with the
// inner array directly. To reverse it, for example, with the
// [`std::slice::reverse`] function:
gridnums.as_mut.reverse;