Skip to content

sncxyz/grid

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

50 Commits
 
 
 
 
 
 
 
 

Repository files navigation

A simple 2D grid library, including a generic heap-allocated 2D grid struct and a 2D vector struct.

For a position Vector { x, y } in the grid:

  • x determines which column the position is in
  • y determines which row the position is in

There are width columns and height rows in the grid, and the grid's iterators traverse it in row-major order.

Usage

Add this to your Cargo.toml:

[dependencies]
grid = { git = "https://github.com/sncxyz/grid" }

Examples

use grid::prelude::*;

let mut grid: Grid<u8> = Grid::new(5, 6, 3);

assert_eq!(grid.width(), 5);
assert_eq!(grid.height(), 6);

grid[v(1, 0)] = 1;
grid[v(3, 5)] = 2;

assert_eq!(grid[v(3, 5)], 2);
assert_eq!(grid[v(1, 0)], 1);
assert_eq!(grid[v(2, 4)], 3);

println!("{:?}", grid);

let mut pos = Vector::new(1, 2);
let mut offset = EAST;

while grid.in_bounds(pos) {
    pos += offset;
}

assert_eq!(pos, v(5, 2));

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages