Struct simple_grid::GridIndex
source · pub struct GridIndex(/* private fields */);
Expand description
A struct used for indexing into a grid.
Implementations§
source§impl GridIndex
impl GridIndex
sourcepub fn neighbors(self) -> impl Iterator<Item = Self>
pub fn neighbors(self) -> impl Iterator<Item = Self>
Returns an iterator over the cardinal and ordinal neighbors of self
.
Returns the neighbors in clockwise order: [up, up_right, right, down_right, down, down_left, left, up_left]
.
Example
let idx = GridIndex::new(0, 1);
let neighbors: Vec<_> = idx.neighbors().collect();
assert_eq!(neighbors, vec![
(0, 0).into(), // up
(1, 0).into(), // up_right
(1, 1).into(), // right
(1, 2).into(), // down_right
(0, 2).into(), // down
// nothing to the left since `idx` has column=0
]);
sourcepub fn cardinal_neighbors(self) -> impl Iterator<Item = Self>
pub fn cardinal_neighbors(self) -> impl Iterator<Item = Self>
Returns an iterator over the cardinal neighbors of self
.
Returns the neighbors in clockwise order: [up, right, down, left]
.
Example
let idx = GridIndex::new(0, 1);
let neighbors: Vec<_> = idx.cardinal_neighbors().collect();
assert_eq!(neighbors, vec![
(0, 0).into(), // up
(1, 1).into(), // right
(0, 2).into(), // down
// nothing to the left since `idx` has column=0
]);
sourcepub fn up(&self) -> Option<Self>
pub fn up(&self) -> Option<Self>
Get the GridIndex
above, if it exists.
Example
let row_5 = GridIndex::new(17, 5);
assert_eq!(row_5.up(), Some(GridIndex::new(17, 4)));
let row_0 = GridIndex::new(38, 0);
assert_eq!(row_0.up(), None);
sourcepub fn right(&self) -> Option<Self>
pub fn right(&self) -> Option<Self>
Get the GridIndex
to the right, if it exists.
Example
let column_17 = GridIndex::new(17, 11);
assert_eq!(column_17.right(), Some(GridIndex::new(18, 11)));
sourcepub fn down(&self) -> Option<Self>
pub fn down(&self) -> Option<Self>
Get the GridIndex
below, if it exists.
Example
let row_15 = GridIndex::new(3, 15);
assert_eq!(row_15.down(), Some(GridIndex::new(3, 16)));
sourcepub fn left(&self) -> Option<Self>
pub fn left(&self) -> Option<Self>
Get the GridIndex
to the left, if it exists.
0).
Example
let column_9 = GridIndex::new(9, 10);
assert_eq!(column_9.left(), Some(GridIndex::new(8, 10)));
let column_0 = GridIndex::new(0, 10);
assert_eq!(column_0.left(), None);
sourcepub fn up_left(&self) -> Option<Self>
pub fn up_left(&self) -> Option<Self>
Get the GridIndex
above and to the left, if it exists.
Example
let column_5_row_4 = GridIndex::new(5, 4);
assert_eq!(column_5_row_4.up_left(), Some(GridIndex::new(4, 3)));
let column_0_row_4 = GridIndex::new(0, 4);
assert_eq!(column_0_row_4.up_left(), None);
sourcepub fn up_right(&self) -> Option<Self>
pub fn up_right(&self) -> Option<Self>
Get the GridIndex
above and to the right, if it exists.
Example
let column_5_row_4 = GridIndex::new(5, 4);
assert_eq!(column_5_row_4.up_right(), Some(GridIndex::new(6, 3)));
let column_5_row_0 = GridIndex::new(5, 0);
assert_eq!(column_5_row_0.up_right(), None);
sourcepub fn down_right(&self) -> Option<Self>
pub fn down_right(&self) -> Option<Self>
Get the GridIndex
below and to the right, if it exists.
Example
let column_5_row_4 = GridIndex::new(5, 4);
assert_eq!(column_5_row_4.down_right(), Some(GridIndex::new(6, 5)));
sourcepub fn down_left(&self) -> Option<Self>
pub fn down_left(&self) -> Option<Self>
Get the GridIndex
below and to the left, if it exists.
Example
let column_5_row_4 = GridIndex::new(5, 4);
assert_eq!(column_5_row_4.down_left(), Some(GridIndex::new(4, 5)));
let column_0_row_0 = GridIndex::new(0, 0);
assert_eq!(column_0_row_0.down_left(), None);
Trait Implementations§
source§impl PartialEq for GridIndex
impl PartialEq for GridIndex
impl Copy for GridIndex
impl Eq for GridIndex
impl StructuralEq for GridIndex
impl StructuralPartialEq for GridIndex
Auto Trait Implementations§
impl RefUnwindSafe for GridIndex
impl Send for GridIndex
impl Sync for GridIndex
impl Unpin for GridIndex
impl UnwindSafe for GridIndex
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more