Node
Nodes are the fundamental building blocks for pathfinding grids. Each Node represents a discrete point in a 2D space, characterized by its integer X and Y coordinates. Nodes can be valid (traversable) or invalid (obstacles), and they maintain information crucial for pathfinding algorithms like A*.
Key Concepts
- Coordinates: Each node has an
xandyinteger coordinate. - Validity: A node can be
valid(meaning it can be traversed) orinvalid(meaning it's an obstacle). This is crucial for pathfinding algorithms to avoid obstacles. - Neighbors: Nodes store a list of their
neighbours, which are other nodes directly adjacent to them in the grid. This is used to explore possible paths. - Pathfinding Metrics: Nodes store
cost,heuristic, andfunctionvalues, which are typically used in algorithms like A* to evaluate the desirability of a path through that node. - Parent: Each node can have a
parentnode, which is used to reconstruct the optimal path once the destination is reached.
Methods
Node(int x, int y)
Constructs a new Node with the specified X and Y coordinates. By default, a new node is considered valid.
calculateNeighborsFor(List<Node> nodes, Grid grid)
Static method. Iterates through a list of nodes and calculates the neighbors for each node within the context of the given grid. This method populates the neighbours list for each node.
getNodes(int x, int y, Grid grid)
Static method. Generates a list of Node objects for a grid of specified x (width) and y (height) dimensions. This method creates the nodes but does not calculate their neighbors.
tryAddNode(boolean condition, int x, int y, Function<Coord, Node> getNode, List<Node> nodes)
Private static helper method. Attempts to add a node to a list if a given condition is met.
tryAddNodes(Map<Coord, Boolean> map, Function<Coord, Node> getNode, List<Node> nodes)
Private static helper method. Iterates through a map of coordinates and conditions, adding nodes to a list based on those conditions.
getValidNodes(List<Node> nodes)
Static method. Filters a list of nodes and returns a new list containing only the valid nodes.
getInvalidNodes(List<Node> nodes)
Static method. Filters a list of nodes and returns a new list containing only the invalid nodes.
calculateNeighbours(Grid grid)
Calculates and sets the neighbours for this specific node based on its position within the grid.
heuristic(Node target)
Calculates the heuristic cost from this node to a target node. By default, this is the straight-line distance.
distanceTo(Node target)
Calculates the Euclidean distance from this node to a target node.
getCost()
Returns the cost to reach this node from the starting node in a pathfinding algorithm.
setCost(double cost)
Sets the cost to reach this node.
getHeuristic()
Returns the estimated cost from this node to the target node (heuristic).
setHeuristic(double heuristic)
Sets the heuristic cost for this node.
getFunction()
Returns the total function cost (cost + heuristic) for this node.
setFunction(double function)
Sets the total function cost for this node.
getNeighbours()
Returns the list of neighboring nodes.
setNeighbours(List<Node> neighbours)
Sets the list of neighboring nodes, filtering out any null entries.
getParent()
Returns the parent node in the path from the start to this node.
setParent(Node parent)
Sets the parent node for this node.
isValid()
Returns true if the node is valid (traversable), false otherwise.
setValid(boolean valid)
Sets the validity of the node.
isInvalid()
Returns true if the node is invalid (an obstacle), false otherwise.
reverseValidation()
Toggles the validity of the node.
getX()
Returns the X-coordinate of the node.
getY()
Returns the Y-coordinate of the node.
hashCode()
Returns a hash code for the node based on its coordinates.
equals(Object obj)
Compares this node to another object for equality based on their X and Y coordinates.
toString()
Returns a string representation of the node, including its coordinates and validity status.