Struct pathfinding::grid::Grid

source ·
pub struct Grid {
    pub width: usize,
    pub height: usize,
    /* private fields */
}
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§

Create a new empty grid object of the given dimensions, with diagonal mode disabled.

Check if a (possibly removed) vertex belongs to the grid or if it is located outside the grid.

Enable diagonal mode. Diagonal edges will be created between adjacent vertices.

Disable diagonal mode. Only horizontal and vertical edges will be created between adjacent vertices.

Resize the grid to the given dimensions. Return true if this caused any existing vertex to be discarded.

Return the number of positions in this grid.

Return the number of vertices.

Add a new vertex. Return true if the vertex did not previously exist and has been added.

Remove a vertex. Return true if the vertex did previously exist and has been removed.

Add the borders of the grid. Return the number of added vertices.

Remove the borders of the grid. Return the number of removed vertices.

Remove all vertices from the grid. Return true if the grid previously contained at least one vertex.

Fill the grid with all possible vertices. Return true if this caused the addition of at least one vertex.

Return true if the grid contains no vertices.

Return true if no additional vertices can be set (because they are all already set).

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.

Check if a vertex is present.

Check if an edge is present.

Iterate over edges.

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.

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.

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.

Iterate over vertices.

Distance between two potential vertices. If diagonal mode is enabled, this is the maximum of both coordinates difference. If diagonal mode is disabled, this is the Manhattan distance.

Trait Implementations§

Returns a copy of the value. Read more
Performs copy-assignment from source. Read more
Formats the value using the given formatter. Read more
Converts to this type from the input type.
Converts to this type from the input type.
Creates a value from an iterator. Read more
The type of the elements being iterated over.
Which kind of iterator are we turning this into?
Creates an iterator from a value. Read more
The type of the elements being iterated over.
Which kind of iterator are we turning this into?
Creates an iterator from a value. Read more
This method tests for self and other values to be equal, and is used by ==. Read more
This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason. Read more

Auto Trait Implementations§

Blanket Implementations§

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more
Compare self to key and return true if they are equal.

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The resulting type after obtaining ownership.
Creates owned data from borrowed data, usually by cloning. Read more
Uses borrowed data to replace owned data, usually by cloning. Read more
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.