[−][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
.
Both x and y coordinates wrap around the limits of the grid.
WrappingCoords2d
is not a container; it is just a tool to manipulate indices.
For a 2D container, see array2d
.
For coordinate translation without wrapping, see ameda
.
WrappingCoords2d
is useful to design cellular automata and agent-based models.
You can use WrappingCoords2d
as part of an Entity-Component-System (ECS)
software architecture for high-performing, flexible models.
See my ABM project for an example.
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. |