8 releases

0.2.3 Sep 5, 2023
0.2.2 Sep 4, 2023
0.2.1 Aug 19, 2023
0.1.5 Aug 12, 2023
0.1.3 Mar 18, 2023

#589 in Data structures

Download history 35/week @ 2023-12-14 13/week @ 2023-12-21 1/week @ 2023-12-28 1/week @ 2024-01-04 2/week @ 2024-01-11 1/week @ 2024-01-18 7/week @ 2024-01-25 4/week @ 2024-02-01 3/week @ 2024-02-08 9/week @ 2024-02-15 26/week @ 2024-02-22 33/week @ 2024-02-29 16/week @ 2024-03-07 50/week @ 2024-03-14 31/week @ 2024-03-21 37/week @ 2024-03-28

135 downloads per month
Used in 4 crates (2 directly)

MIT/Apache

13KB
260 lines

grids Crate

This crate provides a simple and flexible 2D grid data structure mainly intended for grid based games.

If you need a matrix this is not the right crate.

Features

  • Flexible grid creation.
  • Cloning sub-sections of the grid based on rectangular bounds.
  • Pasting one grid into another at a specified offset.
  • Many convenient iterators with/without coordinates in various types.
  • Clamped access methods to ensure coordinates remain within grid boundaries.
  • Support for serialization and deserialization via serde feature.
  • Uses glam and allows indexing with IVec2/UVec2 for extra convenience.

Example Usage

Here's a simple example showcasing the usage of the crate:

let mut grid = Grid::new(3, 2, 0); // A 3x2 grid filled with zeros.
grid[(0, 1)] = 5;

// Accessing using glam::IVec2.
assert_eq!(grid[glam::IVec2::new(1, 0)], 0);
// Accessing using glam::UVec2.
assert_eq!(grid[glam::UVec2::new(0, 1)], 5);

// Converting grid to a Vec.
assert_eq!(
    grid.into_iter_values().collect::<Vec<_>>(),
    vec![0, 0, 0, 5, 0, 0]
);

Dependencies

~3MB
~90K SLoC