Struct controlled_astar::node::Node

source ·
pub struct Node {
    pub x: usize,
    pub y: usize,
    pub is_blocked: bool,
    pub neighbors: BTreeMap<Direction, Option<(usize, usize)>>,
}
Expand description

Represents a node on a map.

Fields§

§x: usize§y: usize§is_blocked: bool§neighbors: BTreeMap<Direction, Option<(usize, usize)>>

Implementations§

source§

impl Node

source

pub fn new( x: usize, y: usize, is_blocked: bool, max_x: usize, max_y: usize ) -> Self

Creates a new Node and initializes neighbors for the four basic directions.

§Parameters
  • x: The x-coordinate of the node.
  • y: The y-coordinate of the node.
  • is_blocked: Indicates whether the node is blocked.
  • max_x: The maximum x dimension of the map.
  • max_y: The maximum y dimension of the map.
§Returns

A newly created Node instance.

§Examples
use my_crate::Node;

// Create a new `Node` at position (2, 3) which is not blocked
let node = Node::new(2, 3, false, 10, 10);

// Check the node's coordinates and blocked status
assert_eq!(node.x, 2);
assert_eq!(node.y, 3);
assert_eq!(node.is_blocked, false);
source

pub fn set_neighbor( &mut self, direction: Direction, neighbor_pos: Option<(usize, usize)> )

Sets the neighbor position for a specific direction.

§Parameters
  • direction: The direction for which to set the neighbor.
  • neighbor_pos: The position of the neighbor.
§Example
let mut node = Node::new(0, 0, false, 10, 10);
node.set_neighbor(Direction::North, Some((0, 1)));
source

pub fn remove_neighbor(&mut self, direction: Direction)

Removes the neighbor for a specific direction.

§Parameters
  • direction: The direction for which to remove the neighbor.
§Example
let mut node = Node::new(0, 0, false, 10, 10);
node.remove_neighbor(Direction::North);
source

pub fn set_blocked(&mut self, blocked: bool)

Sets whether the node is blocked or not.

§Parameters
  • blocked: The blocked status.
§Example
let mut node = Node::new(0, 0, false, 10, 10);
node.set_blocked(true);
source

pub fn get_directions(&self) -> Vec<Direction>

Returns a vector of directions where neighbors are present.

§Returns

A vector containing the directions of neighbors.

§Example
let node = Node::new(0, 0, false, 10, 10);
let directions = node.get_directions();
source

pub fn grid_to_nodes(grid: &[Vec<i32>]) -> HashMap<(usize, usize), Node>

Converts a 2D grid into Node objects.

§Parameters
  • grid: The 2D grid where 1 represents a blocked node and 0 represents a free node.
§Returns

A HashMap containing Node objects mapped by their positions.

§Example
let grid = vec![
    vec![0, 1, 0],
    vec![0, 0, 1],
];
let nodes = Node::grid_to_nodes(&grid);
source

pub fn print_grid(grid: &[Vec<i32>], path: &Option<Vec<(usize, usize)>>)

Prints a 2D grid and an optional path to the screen.

§Parameters
  • grid: The 2D grid.
  • path: An optional vector representing the path to be printed on the grid.
§Example
let grid = vec![
    vec![0, 0, 0],
    vec![0, 1, 0],
];
let path = Some(vec![(0, 0), (1, 1)]);
Node::print_grid(&grid, &path);

Trait Implementations§

source§

impl Clone for Node

source§

fn clone(&self) -> Node

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Debug for Node

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl Hash for Node

source§

fn hash<H: Hasher>(&self, state: &mut H)

Feeds this value into the given Hasher. Read more
1.3.0 · source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
source§

impl PartialEq for Node

source§

fn eq(&self, other: &Node) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl Eq for Node

source§

impl StructuralPartialEq for Node

Auto Trait Implementations§

§

impl Freeze for Node

§

impl RefUnwindSafe for Node

§

impl Send for Node

§

impl Sync for Node

§

impl Unpin for Node

§

impl UnwindSafe for Node

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

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

source§

impl<T> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.