[][src]Crate wrapping_coords2d

Translate between 1D indices and 2D coordinates with wrapping. Use WrappingCoords2d to store data from a 2D grid into a 1D container such as std::vec::Vec. This is not a container; it is just a tool to manipulate indices. For a 2D container, see [array2d]: https://docs.rs/array2d/0.2.1/array2d/ For coordinate translation without wrapping, see [ameda]: https://docs.rs/ameda/0.1.1/ameda/

WrappingCoords2d is useful to design cellular automata and agent-based models. See my [ABM project]: https://github.com/facorread/rust-agent-based-models

Index operations wrap around in both dimensions. See the examples below.

Examples

use wrapping_coords2d::WrappingCoords2d;
let w2d = WrappingCoords2d::new(10, 10).unwrap();
// Here are some basic coordinates:
assert_eq!(w2d.coords(0), (0, 0));
assert_eq!(w2d.coords(1), (1, 0));
assert_eq!(w2d.coords(10), (0, 1));
assert_eq!(w2d.coords(11), (1, 1));
assert_eq!(w2d.coords(90), (0, 9));
assert_eq!(w2d.coords(91), (1, 9));
// Here are the cell at (5, 9) and its 8 neighbors, counterclockwise, starting from the right neighbor:
assert_eq!(w2d.index(5, 9), 95);
assert_eq!(w2d.index(6, 9), 96);
assert_eq!(w2d.index(6, 0), 6);
assert_eq!(w2d.index(5, 0), 5);
assert_eq!(w2d.index(4, 0), 4);
assert_eq!(w2d.index(4, 9), 94);
assert_eq!(w2d.index(4, 8), 84);
assert_eq!(w2d.index(5, 8), 85);
assert_eq!(w2d.index(6, 8), 86);
// Here are the cell at (0, 0) and its 8 neighbors, counterclockwise, starting from the right neighbor:
assert_eq!(w2d.index(0, 0), 0);
assert_eq!(w2d.index(1, 0), 1);
assert_eq!(w2d.index(1, 1), 11);
assert_eq!(w2d.index(0, 1), 10);
assert_eq!(w2d.index(-1, 1), 19);
assert_eq!(w2d.index(-1, 0), 9);
assert_eq!(w2d.index(-1, -1), 99);
assert_eq!(w2d.index(0, -1), 90);
assert_eq!(w2d.index(1, -1), 91);
// Here are the 8 neighbors of the cell at (5, 9), counterclockwise, starting from the right neighbor:
assert_eq!(w2d.shift(95, 1, 0), 96);
assert_eq!(w2d.shift(95, 1, 1), 6);
assert_eq!(w2d.shift(95, 0, 1), 5);
assert_eq!(w2d.shift(95, -1, 1), 4);
assert_eq!(w2d.shift(95, -1, 0), 94);
assert_eq!(w2d.shift(95, -1, -1), 84);
assert_eq!(w2d.shift(95, 0, -1), 85);
assert_eq!(w2d.shift(95, 1, -1), 86);
// Here are the 8 neighbors of the cell at (0, 0), counterclockwise, starting from the right neighbor:
assert_eq!(w2d.shift(0, 1, 0), 1);
assert_eq!(w2d.shift(0, 1, 1), 11);
assert_eq!(w2d.shift(0, 0, 1), 10);
assert_eq!(w2d.shift(0, -1, 1), 19);
assert_eq!(w2d.shift(0, -1, 0), 9);
assert_eq!(w2d.shift(0, -1, -1), 99);
assert_eq!(w2d.shift(0, 0, -1), 90);
assert_eq!(w2d.shift(0, 1, -1), 91);

Structs

WrappingCoords2d

Represents a 2D grid with wrapping.

Enums

ErrorKind

Represents errors in the construction of a 2D grid.