Struct sark_grids::grid::Grid
source · pub struct Grid<T> { /* private fields */ }
Expand description
A dense sized grid that stores it’s elements in a Vec
.
Implementations§
source§impl<T> Grid<T>
impl<T> Grid<T>
sourcepub fn new(size: impl Size2d) -> Selfwhere
T: Default + Clone,
pub fn new(size: impl Size2d) -> Selfwhere T: Default + Clone,
Creates a new Grid
pub fn filled(value: T, size: impl Size2d) -> Selfwhere T: Clone,
sourcepub fn insert_row(&mut self, y: usize, row: impl DoubleEndedIterator<Item = T>)
pub fn insert_row(&mut self, y: usize, row: impl DoubleEndedIterator<Item = T>)
Insert into a row of the grid using an iterator.
Will insert up to the length of a row.
sourcepub fn insert_row_at(&mut self, xy: impl GridPoint, row: impl Iterator<Item = T>)
pub fn insert_row_at(&mut self, xy: impl GridPoint, row: impl Iterator<Item = T>)
Insert into a row of the grid using an iterator.
Will insert up to the length of a row.
sourcepub fn insert_column(&mut self, x: usize, column: impl IntoIterator<Item = T>)
pub fn insert_column(&mut self, x: usize, column: impl IntoIterator<Item = T>)
Insert into a column of the grid using an iterator.
Will insert up to the height of a column.
sourcepub fn insert_column_at(
&mut self,
xy: impl GridPoint,
column: impl IntoIterator<Item = T>
)
pub fn insert_column_at( &mut self, xy: impl GridPoint, column: impl IntoIterator<Item = T> )
Insert into a column of the grid using an iterator.
Will insert up to the height of a column.
pub fn width(&self) -> usize
pub fn height(&self) -> usize
pub fn size(&self) -> UVec2
sourcepub fn pivot_position(&self, pivot: Pivot) -> IVec2
pub fn pivot_position(&self, pivot: Pivot) -> IVec2
Get the position of the given pivot point on the grid.
sourcepub fn get(&self, xy: impl GridPoint) -> Option<&T>
pub fn get(&self, xy: impl GridPoint) -> Option<&T>
Try to retrieve the value at the given position.
Returns None
if the position is out of bounds.
sourcepub fn get_mut(&mut self, xy: impl GridPoint) -> Option<&mut T>
pub fn get_mut(&mut self, xy: impl GridPoint) -> Option<&mut T>
Try to retrieve the mutable value at the given position.
Returns None
if the position is out of bounds.
pub fn in_bounds(&self, pos: impl GridPoint) -> bool
sourcepub fn side_index(&self, side: Side) -> usize
pub fn side_index(&self, side: Side) -> usize
Gets the index for a given side.
pub fn axis_size(&self, axis: usize) -> usize
sourcepub fn iter(&self) -> impl DoubleEndedIterator<Item = &T>
pub fn iter(&self) -> impl DoubleEndedIterator<Item = &T>
An iterator over all elements in the grid.
sourcepub fn iter_mut(&mut self) -> impl DoubleEndedIterator<Item = &mut T>
pub fn iter_mut(&mut self) -> impl DoubleEndedIterator<Item = &mut T>
A mutable iterator over all elements in the grid.
sourcepub fn iter_row(&self, y: usize) -> impl DoubleEndedIterator<Item = &T>
pub fn iter_row(&self, y: usize) -> impl DoubleEndedIterator<Item = &T>
An iterator over a single row of the grid.
Goes from left to right.
sourcepub fn iter_row_mut(
&mut self,
y: usize
) -> impl DoubleEndedIterator<Item = &mut T>
pub fn iter_row_mut( &mut self, y: usize ) -> impl DoubleEndedIterator<Item = &mut T>
A mutable iterator over a single row of the grid.
Iterates from left to right.
sourcepub fn iter_rows(
&self,
range: impl RangeBounds<usize>
) -> impl DoubleEndedIterator<Item = &[T]>
pub fn iter_rows( &self, range: impl RangeBounds<usize> ) -> impl DoubleEndedIterator<Item = &[T]>
Iterate over a range of rows.
Yields &[T] (Slice of T)
sourcepub fn iter_rows_mut(
&mut self,
range: impl RangeBounds<usize>
) -> impl DoubleEndedIterator<Item = &mut [T]>
pub fn iter_rows_mut( &mut self, range: impl RangeBounds<usize> ) -> impl DoubleEndedIterator<Item = &mut [T]>
Iterate mutably over a range of rows.
Yields &mut [T
] (Slice of mutable T
)
sourcepub fn iter_column(&self, x: usize) -> impl DoubleEndedIterator<Item = &T>
pub fn iter_column(&self, x: usize) -> impl DoubleEndedIterator<Item = &T>
An iterator over a single column of the grid.
Goes from bottom to top.
sourcepub fn iter_column_mut(
&mut self,
x: usize
) -> impl DoubleEndedIterator<Item = &mut T>
pub fn iter_column_mut( &mut self, x: usize ) -> impl DoubleEndedIterator<Item = &mut T>
A mutable iterator over a single column of the grid.
Goes from bottom to top.
sourcepub fn axis_index(&self, axis: usize) -> usize
pub fn axis_index(&self, axis: usize) -> usize
Final index along a given axis, where 0 == width, and 1 == height.
sourcepub fn rect_iter(
&self,
range: impl RangeBounds<[i32; 2]>
) -> impl Iterator<Item = (IVec2, &T)>
pub fn rect_iter( &self, range: impl RangeBounds<[i32; 2]> ) -> impl Iterator<Item = (IVec2, &T)>
An iterator over a rectangular portion of the grid defined by the given range.
Yields (IVec2, &T)
, where IVec2
is the corresponding position of the value in the grid.
sourcepub fn iter_2d(&self) -> impl Iterator<Item = (IVec2, &T)>
pub fn iter_2d(&self) -> impl Iterator<Item = (IVec2, &T)>
Returns an iterator which enumerates the 2d position of every value in the grid.
Yields (IVec2, &T)
, where IVec2
is the corresponding position of the value in the grid.
sourcepub fn iter_2d_mut(&mut self) -> impl Iterator<Item = (IVec2, &mut T)>
pub fn iter_2d_mut(&mut self) -> impl Iterator<Item = (IVec2, &mut T)>
Returns a mutable iterator which enumerates the 2d position of every value in the grid.
Yields (IVec2, &mut T)
, where IVec2
is the corresponding position of the value in the grid.
sourcepub fn slice_mut(&mut self) -> &mut [T] ⓘ
pub fn slice_mut(&mut self) -> &mut [T] ⓘ
Retrieve a mutable linear slice of the underlying grid data.
sourcepub fn transform_lti(&self, xy: impl GridPoint) -> usize
pub fn transform_lti(&self, xy: impl GridPoint) -> usize
Converts a 2d grid position to it’s corresponding 1D index. If a pivot was applied to the given grid point, it will be accounted for.
sourcepub fn transform_itl(&self, index: usize) -> IVec2
pub fn transform_itl(&self, index: usize) -> IVec2
Converts a 1d index to it’s corresponding grid position.
sourcepub fn transform_ltw(&self, xy: impl GridPoint) -> IVec2
pub fn transform_ltw(&self, xy: impl GridPoint) -> IVec2
Convert a point from grid space (bottom-left origin) to world space (center origin).
sourcepub fn transform_wtl(&self, xy: impl GridPoint) -> IVec2
pub fn transform_wtl(&self, xy: impl GridPoint) -> IVec2
Convert a point from world space (center origin) to grid space (bottom-left origin).
sourcepub fn pivoted_point(&self, xy: impl GridPoint) -> IVec2
pub fn pivoted_point(&self, xy: impl GridPoint) -> IVec2
Retrieve a grid position from a pivoted point.
If no pivot has been applied, the point will be returned directly.