Struct pathfinding::grid::Grid
source · Expand description
Representation of a rectangular grid in which vertices can be added or removed. Edges are automatically created between adjacent vertices. By default, only vertical and horizontal edges are created, unless diagonal mode is enabled.
Internally, a Grid is represented either as a collection of vertices or as a collection of absent vertices, depending on the density of the grid. The switch between both representations is done automatically when vertices are added or removed, or when the grid is resized.
Grid
implements Debug
and represents the content using #
and .
characters. Alternate block characters ▓
and ░
can be selected by
using the alternate debug format ({:#?}
):
use pathfinding::prelude::Grid;
let mut g = Grid::new(3, 4);
g.add_borders();
assert_eq!(&format!("{:?}", g), "\
###
#.#
#.#
###");
assert_eq!(&format!("{:#?}", g), "\
▓▓▓
▓░▓
▓░▓
▓▓▓");
Fields§
§width: usize
The grid width.
height: usize
The grid height.
Implementations§
source§impl Grid
impl Grid
sourcepub fn new(width: usize, height: usize) -> Self
pub fn new(width: usize, height: usize) -> Self
Create a new empty grid object of the given dimensions, with diagonal mode disabled.
sourcepub fn is_inside(&self, vertex: (usize, usize)) -> bool
pub fn is_inside(&self, vertex: (usize, usize)) -> bool
Check if a (possibly removed) vertex belongs to the grid or if it is located outside the grid.
sourcepub fn enable_diagonal_mode(&mut self)
pub fn enable_diagonal_mode(&mut self)
Enable diagonal mode. Diagonal edges will be created between adjacent vertices.
sourcepub fn disable_diagonal_mode(&mut self)
pub fn disable_diagonal_mode(&mut self)
Disable diagonal mode. Only horizontal and vertical edges will be created between adjacent vertices.
sourcepub fn resize(&mut self, width: usize, height: usize) -> bool
pub fn resize(&mut self, width: usize, height: usize) -> bool
Resize the grid to the given dimensions. Return true
if this
caused any existing vertex to be discarded.
sourcepub fn vertices_len(&self) -> usize
pub fn vertices_len(&self) -> usize
Return the number of vertices.
sourcepub fn add_vertex(&mut self, vertex: (usize, usize)) -> bool
pub fn add_vertex(&mut self, vertex: (usize, usize)) -> bool
Add a new vertex. Return true
if the vertex did not previously
exist and has been added.
sourcepub fn remove_vertex(&mut self, vertex: (usize, usize)) -> bool
pub fn remove_vertex(&mut self, vertex: (usize, usize)) -> bool
Remove a vertex. Return true
if the vertex did previously exist
and has been removed.
sourcepub fn add_borders(&mut self) -> usize
pub fn add_borders(&mut self) -> usize
Add the borders of the grid. Return the number of added vertices.
sourcepub fn remove_borders(&mut self) -> usize
pub fn remove_borders(&mut self) -> usize
Remove the borders of the grid. Return the number of removed vertices.
sourcepub fn clear(&mut self) -> bool
pub fn clear(&mut self) -> bool
Remove all vertices from the grid. Return true
if the grid
previously contained at least one vertex.
sourcepub fn fill(&mut self) -> bool
pub fn fill(&mut self) -> bool
Fill the grid with all possible vertices. Return true
if
this caused the addition of at least one vertex.
sourcepub fn is_full(&self) -> bool
pub fn is_full(&self) -> bool
Return true
if no additional vertices can be set
(because they are all already set).
sourcepub fn invert(&mut self)
pub fn invert(&mut self)
Remove every existing vertex, and add all absent vertices. If you see the grid as a black and white array, imagine that the color are exchanged.
sourcepub fn has_vertex(&self, vertex: (usize, usize)) -> bool
pub fn has_vertex(&self, vertex: (usize, usize)) -> bool
Check if a vertex is present.
sourcepub fn has_edge(&self, v1: (usize, usize), v2: (usize, usize)) -> bool
pub fn has_edge(&self, v1: (usize, usize), v2: (usize, usize)) -> bool
Check if an edge is present.
sourcepub fn edges(&self) -> EdgesIterator<'_> ⓘ
pub fn edges(&self) -> EdgesIterator<'_> ⓘ
Iterate over edges.
sourcepub fn neighbours(&self, vertex: (usize, usize)) -> Vec<(usize, usize)>
pub fn neighbours(&self, vertex: (usize, usize)) -> Vec<(usize, usize)>
Return the list of neighbours of a given vertex. If vertex
is absent
from the grid, an empty list is returned. Only existing vertices will
be returned.
sourcepub fn bfs_reachable<P>(
&self,
start: (usize, usize),
predicate: P
) -> BTreeSet<(usize, usize)>where
P: FnMut((usize, usize)) -> bool,
pub fn bfs_reachable<P>(
&self,
start: (usize, usize),
predicate: P
) -> BTreeSet<(usize, usize)>where
P: FnMut((usize, usize)) -> bool,
Return a set of the indices reachable from a candidate starting point and for which the given predicate is valid using BFS. This can be used for example to implement a flood-filling algorithm. Since the indices are collected into a collection, they can later be used without keeping a reference on the matrix itself, e.g., to modify the grid.
The search is done using a breadth first search (BFS) algorithm.
See also
The dfs_reachable()
performs a DFS search instead.
sourcepub fn dfs_reachable<P>(
&self,
start: (usize, usize),
predicate: P
) -> BTreeSet<(usize, usize)>where
P: FnMut((usize, usize)) -> bool,
pub fn dfs_reachable<P>(
&self,
start: (usize, usize),
predicate: P
) -> BTreeSet<(usize, usize)>where
P: FnMut((usize, usize)) -> bool,
Return a set of the indices reachable from a candidate starting point and for which the given predicate is valid using BFS. This can be used for example to implement a flood-filling algorithm. Since the indices are collected into a collection, they can later be used without keeping a reference on the matrix itself, e.g., to modify the grid.
The search is done using a depth first search (DFS) algorithm.
See also
The bfs_reachable()
performs a BFS search instead.
sourcepub fn iter(&self) -> GridIterator<'_> ⓘ
pub fn iter(&self) -> GridIterator<'_> ⓘ
Iterate over vertices.
Trait Implementations§
source§impl<'a> IntoIterator for &'a Grid
impl<'a> IntoIterator for &'a Grid
source§impl IntoIterator for Grid
impl IntoIterator for Grid
impl Eq for Grid
Auto Trait Implementations§
impl RefUnwindSafe for Grid
impl Send for Grid
impl Sync for Grid
impl Unpin for Grid
impl UnwindSafe for Grid
Blanket Implementations§
source§impl<Q, K> Equivalent<K> for Qwhere
Q: Eq + ?Sized,
K: Borrow<Q> + ?Sized,
impl<Q, K> Equivalent<K> for Qwhere
Q: Eq + ?Sized,
K: Borrow<Q> + ?Sized,
source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key
and return true
if they are equal.