Trait Grid2D

Source
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§

Source

fn dimensions(&self) -> Point2D

The dimensions of the Grid. The only method that must be defined.

Provided Methods§

Source

fn lower_bound(&self) -> Point2D

The lower bound. Defaults to (0, 0).

Source

fn upper_bound(&self) -> Point2D

The upper bound. Defaults to the dimensions itself.

Source

fn in_bounds(&self, point: Point2D) -> bool

Check if a point is in the bounds of the grid.

Source

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.

Source

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.

Source

fn is_opaque(&self, point: Point2D) -> bool

Check if a point is traversable.

Defaults to always, so you may want to implement this.

Source

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.

Source

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.

Source

fn get_neighbors(&self, point: Point2D) -> [Option<Point2D>; 8]

Get the neighbors that is in bounds and not opaque.

Source

fn is_neighbor(&self, p1: Point2D, p2: Point2D) -> bool

Check if two points are neighbors.

Checks if either points are inbounds or non-opaque.

Source

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.

Implementors§