Skip to content

Instantly share code, notes, and snippets.

@LucasAlfare
Last active May 29, 2023 19:42
Show Gist options
  • Save LucasAlfare/1117a456ad1a0d2fb699096ef057aaa4 to your computer and use it in GitHub Desktop.
Save LucasAlfare/1117a456ad1a0d2fb699096ef057aaa4 to your computer and use it in GitHub Desktop.
Draft of pathfinding, in Javascript.
/**
* Class used to represent a single coordinate of a two-dimensional space.
*
* Also, this coord should be used in context where the actual coordinate system
* lies in the 4-th quadrant.
*/
class Coordinate {
/**
* Creates a new coordinate object with x and y coordinates based on params.
* @param {number} x the x coordinate to this object.
* @param {number} y the y coordinate to this object.
*/
constructor(x, y) {
this.x = x;
this.y = y;
}
/**
* Function used to check if this coodinates pair is inside a rect of dimensions
* width and height.
*
* @param {number} width checking width
* @param {number} height checking height
*
* @returns {boolean} true if this coordinate is inside the bounds, otherwise {boolean} false
*/
inBounds(width, height) {
return (
(x >= 0 && x < width) &&
(y >= 0 && y < height)
);
}
}
export default Coordinate;
import Coordinate from "./Coordinate";
/**
* A node is a representation of a point inside a two-dimensional space.
*
* A node has coordinate and fields to store costs. Also, contains references to
* its parent node, if avaliable.
*/
class Node {
/**
* Creates a new node object with the coord from param.
*
* @param {Coordinate} coordinate
*/
constructor(coordinate) {
this.coordinate = coordinate;
this.f = 0;
this.g = 0;
this.h = 0;
this.parent = null;
}
/**
* Calculates the costs values to this current node, based on
* start and end coordinates.
*
* @param {Coordinate} start the initial coordinate of the path.
* @param {Coordinate} end the targeted end coordiante that the path aims on.
*/
processCosts(start, end) {
this.g = Math.abs(this.coordinate.x - start.x) + Math.abs(this.coordinate.y - start.y);
this.h = Math.abs(this.coordinate.x - end.x) + Math.abs(this.coordinate.y - end.y);
this.f = this.g + this.h;
}
/**
* This method is used to get the actual neighbors (other surrounding nodes)
* of this current node.
* @param {number} width the current width of the space where this node is being processed.
* @param {number} height the current height of the space where this node is being processed.
*
* @returns {Array<Node>} list of the actual neighbors of this current node.
*/
getNeighbors(width, height) {
const results = [];
const north = new Coordinate(this.coordinate.x, this.coordinate.y - 1);
const south = new Coordinate(this.coordinate.x, this.coordinate.y + 1);
const east = new Coordinate(this.coordinate.x + 1, this.coordinate.y);
const west = new Coordinate(this.coordinate.x - 1, this.coordinate.y);
if (north.inBounds(width, height)) results.push(new Node(north));
if (south.inBounds(width, height)) results.push(new Node(south));
if (east.inBounds(width, height)) results.push(new Node(east));
if (west.inBounds(width, height)) results.push(new Node(west));
// err...?
const self = this;
results.forEach(n => { n.parent = self });
return results;
}
}
export default Node;
import Coordinate from "./Coordinate";
/**
* Find the costless path (a.k.a. shortest path) between the
* passed coordinates.
*
* @param {Coordinate} start the initial coordinate that the path starts on.
* @param {Coordinate} end the target end coordinate to the path.
*/
function findThePath(start, end) {
// TODO
}
import Node from "./Node";
const Utils = {
/**
* Function used to remove a Node object from a array of Nodes.
*
* @param {Array<Node>} nodes the array of elements containing the targetNode
* @param {Node} targetNode the node to be removed from [nodes].
*/
removeNode: function (nodes, targetNode) {
const targetNodeIndex = nodes.findIndex((n) =>
(n.coordinate.x === targetNode.coordinate.x && n.coordinate.y === targetNode.coordinate.y
));
// takes effect to the array or must be returned?
nodes.splice(targetNodeIndex, 1);
},
/**
* This function sorts all nodes of the param array by their numeric [f]
* value.
*
* @param {Array<Node>} nodes the source array of nodes to be sorted.
*/
sortNodesByFCost: function(nodes) {
// TODO
}
};
export default Utils;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment