pub trait Grid2D {
// Required method
fn dimensions(&self) -> Point2D;
// Provided methods
fn lower_bound(&self) -> Point2D { ... }
fn upper_bound(&self) -> Point2D { ... }
fn in_bounds(&self, point: Point2D) -> bool { ... }
fn point2d_to_index(&self, point: Point2D) -> usize { ... }
fn index_to_point2d(&self, index: usize) -> Point2D { ... }
fn is_opaque(&self, point: Point2D) -> bool { ... }
fn get_possible_neighbors(&self, point: Point2D) -> [Point2D; 8] { ... }
fn is_possible_neighbor(&self, p1: Point2D, p2: Point2D) -> bool { ... }
fn get_neighbors(&self, point: Point2D) -> [Option<Point2D>; 8] { ... }
fn is_neighbor(&self, p1: Point2D, p2: Point2D) -> bool { ... }
fn get_neighbors_with_cost(
&self,
point: Point2D,
) -> [(Option<Point2D>, BigRational); 8] { ... }
}Expand description
Trait for implementing a Grid in two dimensions
Required Methods§
Sourcefn dimensions(&self) -> Point2D
fn dimensions(&self) -> Point2D
The dimensions of the Grid. The only method that must be defined.
Provided Methods§
Sourcefn lower_bound(&self) -> Point2D
fn lower_bound(&self) -> Point2D
The lower bound. Defaults to (0, 0).
Sourcefn upper_bound(&self) -> Point2D
fn upper_bound(&self) -> Point2D
The upper bound. Defaults to the dimensions itself.
Sourcefn point2d_to_index(&self, point: Point2D) -> usize
fn point2d_to_index(&self, point: Point2D) -> usize
Convert a point to an index.
Useful if you store the grid in a one-dimensional array.
Sourcefn index_to_point2d(&self, index: usize) -> Point2D
fn index_to_point2d(&self, index: usize) -> Point2D
Convert an index to a point.
Useful if you store the grid in a one-dimensional array.
Sourcefn is_opaque(&self, point: Point2D) -> bool
fn is_opaque(&self, point: Point2D) -> bool
Check if a point is traversable.
Defaults to always, so you may want to implement this.
Sourcefn get_possible_neighbors(&self, point: Point2D) -> [Point2D; 8]
fn get_possible_neighbors(&self, point: Point2D) -> [Point2D; 8]
Get all possible neighbors of the point, regardless if the point or its neighbors is in bounds, opaque, or neither.
Sourcefn is_possible_neighbor(&self, p1: Point2D, p2: Point2D) -> bool
fn is_possible_neighbor(&self, p1: Point2D, p2: Point2D) -> bool
Check if two points are possible neighbors.
Does not check if either points are inbounds or non-opaque.
Sourcefn get_neighbors(&self, point: Point2D) -> [Option<Point2D>; 8]
fn get_neighbors(&self, point: Point2D) -> [Option<Point2D>; 8]
Get the neighbors that is in bounds and not opaque.
Sourcefn is_neighbor(&self, p1: Point2D, p2: Point2D) -> bool
fn is_neighbor(&self, p1: Point2D, p2: Point2D) -> bool
Check if two points are neighbors.
Checks if either points are inbounds or non-opaque.
Sourcefn get_neighbors_with_cost(
&self,
point: Point2D,
) -> [(Option<Point2D>, BigRational); 8]
fn get_neighbors_with_cost( &self, point: Point2D, ) -> [(Option<Point2D>, BigRational); 8]
Get the neighbors with the associated cost.
Defaults to all eight neighbors having a cost of 1.0 if the neighbor is valid.
If you want the diagonals to cost sqrt(2), reimplement this method yourself.
Dyn Compatibility§
This trait is dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".