### Create Grid and Find Path with A* Source: https://github.com/qiao/pathfinding.js/blob/master/docs/user-guide/getting-started.md Illustrates the process of creating a grid, instantiating an A* finder, and finding a path between two points on the grid. ```javascript var grid = new PF.Grid(5, 7); var finder = new PF.AStarFinder(); var path = finder.findPath(0, 0, 4, 6, grid); ``` ```javascript [[0, 0], [1, 1], [2, 2], [3, 3], [4, 4], [4, 5], [4, 6]] ``` -------------------------------- ### Import PathFinding.js in Node.js Source: https://github.com/qiao/pathfinding.js/blob/master/docs/user-guide/getting-started.md Demonstrates how to import the PathFinding.js library into a Node.js project using the 'require' function. ```javascript var PF = require('pathfinding'); ``` -------------------------------- ### Include PathFinding.js in Browser Source: https://github.com/qiao/pathfinding.js/blob/master/docs/user-guide/getting-started.md Shows how to include the PathFinding.js library in an HTML page using a script tag, typically pointing to a minified browser build. ```html ``` -------------------------------- ### Manual Installation and Compilation for Browser Source: https://github.com/qiao/pathfinding.js/blob/master/docs/user-guide/installation.md Steps for manually installing PathFinding.js from its GitHub repository and compiling it for browser usage. This involves downloading the source, installing dependencies, and using Gulp to create browser-ready JavaScript files. ```bash cd npm install gulp compile ``` -------------------------------- ### Install PathFinding.js with bower Source: https://github.com/qiao/pathfinding.js/blob/master/docs/user-guide/installation.md Installs the PathFinding.js library for front-end projects using the bower package manager. Ensure bower is installed globally (`npm install -g bower`) before running this command from your project's root directory. ```bash bower install pathfinding ``` -------------------------------- ### Install PathFinding.js with npm Source: https://github.com/qiao/pathfinding.js/blob/master/docs/user-guide/installation.md Installs the PathFinding.js library for Node.js projects using the npm package manager. This command should be run from your project's root directory. ```bash npm install pathfinding ``` -------------------------------- ### Install PathFinding.js via Bower Source: https://github.com/qiao/pathfinding.js/blob/master/README.md Installs the pathfinding library for use in browser projects using Bower. ```bash bower install pathfinding ``` -------------------------------- ### Install PathFinding.js via npm Source: https://github.com/qiao/pathfinding.js/blob/master/README.md Installs the pathfinding library for use in Node.js projects. ```bash npm install pathfinding ``` -------------------------------- ### Development Dependencies Installation Source: https://github.com/qiao/pathfinding.js/blob/master/README.md Installs development dependencies for the project using npm. Requires Node.js to be installed. ```bash npm install -d ``` -------------------------------- ### Basic Pathfinding Example Source: https://github.com/qiao/pathfinding.js/blob/master/docs/user-guide/introduction.md Demonstrates how to create a grid from a walkability matrix, initialize an A* pathfinder, and find a path between two points on the grid. The matrix uses 0 for walkable cells and 1 for obstacles. ```javascript var matrix = [ [0, 0, 0, 1, 0], [1, 0, 0, 0, 1], [0, 0, 1, 0, 0], ]; var grid = new PF.Grid(matrix); var finder = new PF.AStarFinder(); //Find path from (1, 2) to (4, 2) var path = finder.findPath(1, 2, 4, 2, grid); ``` -------------------------------- ### Global Gulp Installation Source: https://github.com/qiao/pathfinding.js/blob/master/README.md Installs Gulp globally, which is used as the build system for the project. This command requires administrator privileges on some systems. ```bash npm install -d -g gulp ``` -------------------------------- ### Global Mocha Installation Source: https://github.com/qiao/pathfinding.js/blob/master/README.md Installs Mocha globally, a JavaScript test framework used for running the project's algorithm tests. ```bash npm install -d -g mocha ``` -------------------------------- ### Include PathFinding.js in HTML Source: https://github.com/qiao/pathfinding.js/blob/master/README.md Includes the pathfinding library in an HTML page using a script tag, assuming it's installed via Bower. ```html ``` -------------------------------- ### Find Path using A* Algorithm Source: https://github.com/qiao/pathfinding.js/blob/master/docs/user-guide/obstacles.md Finds a path on the grid using the A* pathfinding algorithm. It takes the start coordinates (startX, startY), end coordinates (endX, endY), and the grid object as input. ```javascript var finder = new PF.AStarFinder(); var path = finder.findPath(0, 0, 4, 6, grid); ``` -------------------------------- ### A* Pathfinding Algorithm Source: https://github.com/qiao/pathfinding.js/wiki/Home Implements the A* search algorithm, a popular and efficient pathfinding method that uses a heuristic to guide its search. It finds the shortest path between two points on a grid. ```javascript class AStar { constructor(grid) { this.grid = grid; this.openSet = new Heap((a, b) => a.f - b.f); this.closedSet = new Set(); this.cameFrom = new Map(); this.gScore = new Map(); this.fScore = new Map(); } heuristic(nodeA, nodeB) { // Manhattan distance return Math.abs(nodeA.x - nodeB.x) + Math.abs(nodeA.y - nodeB.y); } getNeighbors(node) { const neighbors = []; const { x, y } = node; const directions = [ { dx: -1, dy: 0 }, { dx: 1, dy: 0 }, { dx: 0, dy: -1 }, { dx: 0, dy: 1 }, { dx: -1, dy: -1 }, { dx: -1, dy: 1 }, { dx: 1, dy: -1 }, { dx: 1, dy: 1 } ]; for (const dir of directions) { const nx = x + dir.dx; const ny = y + dir.dy; if (nx >= 0 && nx < this.grid.width && ny >= 0 && ny < this.grid.height && this.grid.isWalkable(nx, ny)) { neighbors.push({ x: nx, y: ny }); } } return neighbors; } reconstructPath(current) { const path = []; while (current) { path.push(current); current = this.cameFrom.get(current.toString()); } return path.reverse(); } findPath(start, end) { const startNode = { x: start.x, y: start.y }; const endNode = { x: end.x, y: end.y }; this.openSet.push(startNode); this.gScore.set(startNode.toString(), 0); this.fScore.set(startNode.toString(), this.heuristic(startNode, endNode)); while (this.openSet.size() > 0) { const current = this.openSet.pop(); if (current.x === endNode.x && current.y === endNode.y) { return this.reconstructPath(current); } this.closedSet.add(current.toString()); const neighbors = this.getNeighbors(current); for (const neighbor of neighbors) { if (this.closedSet.has(neighbor.toString())) { continue; } const tentativeGScore = (this.gScore.get(current.toString()) || Infinity) + 1; if (tentativeGScore < (this.gScore.get(neighbor.toString()) || Infinity)) { this.cameFrom.set(neighbor.toString(), current); this.gScore.set(neighbor.toString(), tentativeGScore); this.fScore.set(neighbor.toString(), tentativeGScore + this.heuristic(neighbor, endNode)); if (!this.openSet.data.some(node => node.x === neighbor.x && node.y === neighbor.y)) { this.openSet.push(neighbor); } } } } return []; // No path found } } ``` -------------------------------- ### Run Benchmarks Source: https://github.com/qiao/pathfinding.js/blob/master/README.md Executes the project's performance benchmarks. ```bash gulp bench ``` -------------------------------- ### Build Browser Distribution Source: https://github.com/qiao/pathfinding.js/blob/master/README.md Compiles the project to create the browser distribution files using the Gulp build system. ```bash gulp compile ``` -------------------------------- ### Create a Grid Instance Source: https://github.com/qiao/pathfinding.js/blob/master/README.md Initializes a new PF.Grid object with specified dimensions. ```javascript var grid = new PF.Grid(5, 3); ``` -------------------------------- ### Include PathFinding.js Source: https://github.com/qiao/pathfinding.js/wiki/Home Demonstrates how to include the PathFinding.js library in both Node.js and browser environments. ```javascript var PF = require('pathfinding'); ``` ```html ``` -------------------------------- ### Pathfinding Algorithms Source: https://github.com/qiao/pathfinding.js/wiki/Home Overview of the different pathfinding algorithms available in PathFinding.js, including their configurable options. ```APIDOC AStarFinder: function({allowDiagonal: Boolean, dontCrossCorners: Boolean, heuristic: Function}) BreadthFirstFinder: function({allowDiagonal: Boolean, dontCrossCorners: Boolean}) BestFirstFinder: function({allowDiagonal: Boolean, dontCrossCorners: Boolean, heuristic: Function}) DijkstraFinder: function({allowDiagonal: Boolean, dontCrossCorners: Boolean}) BiAStarFinder: function({allowDiagonal: Boolean, dontCrossCorners: Boolean, heuristic: Function}) BiBestFirstFinder: function({allowDiagonal: Boolean, dontCrossCorners: Boolean, heuristic: Function}) BiDijkstraFinder: function({allowDiagonal: Boolean, dontCrossCorners: Boolean}) BiBreadthFirstFinder: function({allowDiagonal: Boolean, dontCrossCorners: Boolean}) JumpPointFinder: function({heuristic: Function}) OrthogonalJumpPointFinder: function({heuristic: Function}) IDAStarFinder: function({allowDiagonal: Boolean, dontCrossCorners: Boolean, heuristic: function}) ``` -------------------------------- ### Instantiate AStarFinder Source: https://github.com/qiao/pathfinding.js/blob/master/README.md Creates an instance of the AStarFinder path-finding algorithm. ```javascript var finder = new PF.AStarFinder(); ``` -------------------------------- ### BestFirstFinder with Diagonal Movement and Custom Heuristic Source: https://github.com/qiao/pathfinding.js/blob/master/README.md Shows how to initialize a BestFirstFinder, enabling diagonal movement and providing a custom heuristic function (minimum of dx and dy). ```javascript var finder = new PF.BestFirstFinder({ allowDiagonal: true, heuristic: function(dx, dy) { return Math.min(dx, dy); } }); ``` -------------------------------- ### Run Tests Source: https://github.com/qiao/pathfinding.js/blob/master/README.md Executes the project's algorithm tests using Gulp and Mocha. This command does not include visualization tests. ```bash gulp test ``` -------------------------------- ### Pathfinding Algorithms and Options Source: https://github.com/qiao/pathfinding.js/blob/master/visual/index.html This section details the various pathfinding algorithms supported by PathFinding.js, along with their configurable heuristic and option settings. Users can select different heuristics like Manhattan, Euclidean, Octile, and Chebyshev, and enable options such as diagonal movement, bi-directional search, and visualization. ```APIDOC A*: Heuristics: - Manhattan - Euclidean - Octile - Chebyshev Options: - Allow Diagonal - Bi-directional - Don't Cross Corners - Weight IDA*: Heuristics: - Manhattan - Euclidean - Octile - Chebyshev Options: - Allow Diagonal - Don't Cross Corners - Weight - Seconds limit - Visualize recursion Breadth-First-Search: Options: - Allow Diagonal - Bi-directional - Don't Cross Corners Best-First-Search: Heuristics: - Manhattan - Euclidean - Octile - Chebyshev Options: - Allow Diagonal - Bi-directional - Don't Cross Corners Dijkstra: Options: - Allow Diagonal - Bi-directional - Don't Cross Corners Jump Point Search: Heuristics: - Manhattan - Euclidean - Octile - Chebyshev Options: - Visualize recursion Orthogonal Jump Point Search: Heuristics: - Manhattan - Euclidean - Octile - Chebyshev Options: - Visualize recursion ``` -------------------------------- ### Create Grid with Obstacles from Matrix Source: https://github.com/qiao/pathfinding.js/blob/master/docs/user-guide/obstacles.md Initializes a Pathfinding.js grid with obstacles defined by a walkability matrix. '1' represents an obstacle, and '0' represents a walkable cell. ```javascript var walkabilityMatrix = [[0, 0, 0, 0, 0], [1, 1, 1, 1, 0], [0, 0, 0, 0, 0], [0, 1, 1, 1, 1], [0, 0, 0, 0, 0], [1, 1, 1, 1, 0], [0, 0, 0, 0, 0]]; var grid = new PF.Grid(matrix); ``` -------------------------------- ### Create Grid from Matrix Source: https://github.com/qiao/pathfinding.js/blob/master/README.md Initializes a PF.Grid object using a 2D array (matrix) where 0 represents walkable and 1 represents blocked nodes. ```javascript var matrix = [ [0, 0, 0, 1, 0], [1, 0, 0, 0, 1], [0, 0, 1, 0, 0], ]; var grid = new PF.Grid(matrix); ``` -------------------------------- ### Heap Implementation Source: https://github.com/qiao/pathfinding.js/wiki/Home Describes the Heap class, a binary heap implementation used for efficient priority queue operations within pathfinding algorithms. ```APIDOC Heap: A binary heap implementation in CoffeeScript/JavaScript ported from Python's heapq module. ``` -------------------------------- ### Default Gulp Task Source: https://github.com/qiao/pathfinding.js/blob/master/README.md Runs the default Gulp task, which typically includes compiling the project and running tests, but excludes benchmarks. ```bash gulp ``` -------------------------------- ### AStarFinder with Chebyshev Heuristic Source: https://github.com/qiao/pathfinding.js/blob/master/README.md Demonstrates how to configure the AStarFinder to use the Chebyshev heuristic function for pathfinding calculations. ```javascript var finder = new PF.AStarFinder({ heuristic: PF.Heuristic.chebyshev }); ``` -------------------------------- ### AStarFinder with Diagonal Movement Source: https://github.com/qiao/pathfinding.js/blob/master/README.md Initializes an AStarFinder instance allowing diagonal movement. ```javascript var finder = new PF.AStarFinder({ allowDiagonal: true }); ``` -------------------------------- ### Path Expansion with PF.Util.expandPath Source: https://github.com/qiao/pathfinding.js/blob/master/README.md Expands a compressed path back into its original, more detailed form by inserting intermediate waypoints. ```javascript var newPath = PF.Util.expandPath(path); ``` -------------------------------- ### Utility Functions Source: https://github.com/qiao/pathfinding.js/wiki/Home Provides various utility functions for path manipulation, including backtracking, path expansion, and smoothing. ```APIDOC Util: { backtrace: function(node){ return reversed path }, biBacktrace: function(nodeA, nodeB){ return pathA.concat(pathB.reverse) }, expandPath: function(path){ return a new path with all segments interpolated }, interpolate: function(x0, y0, x1, y1){ return a line based on Bresenham's algorithm }, pathLength: function(path){ return path.length }, smoothenPath: function(grid, path){ return a new smoothed path } } ``` -------------------------------- ### Require PathFinding.js in Node.js Source: https://github.com/qiao/pathfinding.js/blob/master/README.md Imports the pathfinding library into a Node.js script. ```javascript var PF = require('pathfinding'); ``` -------------------------------- ### Path Compression with PF.Util.compressPath Source: https://github.com/qiao/pathfinding.js/blob/master/README.md Compresses a given path by removing redundant waypoints, returning a new, shorter path. ```javascript var newPath = PF.Util.compressPath(path); ``` -------------------------------- ### Heap Data Structure Source: https://github.com/qiao/pathfinding.js/wiki/Home A binary heap implementation used for efficient priority queue operations, crucial for algorithms like A* and Dijkstra's. It supports basic heap operations like push, pop, and peek. ```javascript class Heap { constructor(comparator = (a, b) => a - b) { this.data = []; this.comparator = comparator; } push(val) { this.data.push(val); this.bubbleUp(); } pop() { if (this.data.length === 0) return undefined; if (this.data.length === 1) return this.data.pop(); const root = this.data[0]; this.data[0] = this.data.pop(); this.bubbleDown(); return root; } peek() { return this.data[0]; } size() { return this.data.length; } bubbleUp() { let index = this.data.length - 1; while (index > 0) { const parentIndex = Math.floor((index - 1) / 2); if (this.comparator(this.data[index], this.data[parentIndex]) < 0) { [this.data[index], this.data[parentIndex]] = [this.data[parentIndex], this.data[index]]; index = parentIndex; } else { break; } } } bubbleDown() { let index = 0; const length = this.data.length; const element = this.data[0]; while (true) { let leftChildIndex = 2 * index + 1; let rightChildIndex = 2 * index + 2; let leftChild, rightChild; let swap = null; if (leftChildIndex < length) { leftChild = this.data[leftChildIndex]; if (this.comparator(leftChild, element) < 0) { swap = leftChildIndex; } } if (rightChildIndex < length) { rightChild = this.data[rightChildIndex]; if ( (swap === null && this.comparator(rightChild, element) < 0) || (swap !== null && this.comparator(rightChild, leftChild) < 0) ) { swap = rightChildIndex; } } if (swap === null) break; this.data[index] = this.data[swap]; this.data[swap] = element; index = swap; } } } ``` -------------------------------- ### Heuristics Source: https://github.com/qiao/pathfinding.js/wiki/Home Provides common heuristic functions used by pathfinding algorithms to estimate the cost to reach the goal. ```APIDOC Heuristic: { manhattan: function(dx, dy){ return dx + dy } (default) chebyshev: function(dx, dy){ return Math.max(x, y) }, euclidean: function(dx, dy){ return Math.sqrt(dx*dx + dy*dy) } } ``` -------------------------------- ### Path Smoothing with PF.Util.smoothenPath Source: https://github.com/qiao/pathfinding.js/blob/master/README.md Utilizes the `smoothenPath` utility to create a new, smoothed path from an existing grid and path. The original path remains unmodified. ```javascript var newPath = PF.Util.smoothenPath(grid, path); ``` -------------------------------- ### Clone Grid Source: https://github.com/qiao/pathfinding.js/blob/master/README.md Creates a deep copy of the grid object, necessary if the same grid needs to be used for multiple pathfinding operations. ```javascript var gridBackup = grid.clone(); ``` -------------------------------- ### Find Path Source: https://github.com/qiao/pathfinding.js/blob/master/README.md Calculates a path between two points on the grid using the instantiated finder. The grid is modified after this operation. ```javascript var path = finder.findPath(1, 2, 4, 2, grid); ``` -------------------------------- ### Set Obstacles Individually on Grid Source: https://github.com/qiao/pathfinding.js/blob/master/docs/user-guide/obstacles.md Sets individual cells as obstacles or walkable on a Pathfinding.js grid. `setWalkableAt(x, y, walkable)` sets the walkability of the cell at coordinates (x, y). ```javascript var grid = new PF.Grid(5, 7); grid.setWalkableAt(0, 1, false); grid.setWalkableAt(1, 1, false); grid.setWalkableAt(2, 1, false); ... ``` -------------------------------- ### Grid Class Source: https://github.com/qiao/pathfinding.js/wiki/Home Represents the grid on which pathfinding is performed. It stores nodes and provides methods for grid manipulation and neighbor retrieval. ```APIDOC Grid: function(width, height, [walkableMatrix]){ nodes: Array, width: Number, heigth: Number, clone: function (){ return a new grid clone }, getNeighbors: function (node, allowDiagonal, dontCrossCorners){ return walkable neighbors }, getNodeAt: function (x, y){ return grid.nodes[y][x] }, isInside: function (x, y){ return true if point is inside grid area }, isWalkableAt: function (x, y){ return true if point is walkable }, setWalkableAt: function (x, y, Boolean) { grid.nodes[y][x].walkable = Boolean} } ``` -------------------------------- ### Set Node Walkability Source: https://github.com/qiao/pathfinding.js/blob/master/README.md Marks a specific node in the grid as walkable or not walkable. ```javascript grid.setWalkableAt(0, 1, false); ``` -------------------------------- ### Node Class Source: https://github.com/qiao/pathfinding.js/wiki/Home Represents a single node within the grid, storing its position and pathfinding-related properties. ```APIDOC Node: function(x, y, walkable) { x: Number, y: Number, walkable: Boolean, closed: Boolean, opened: Boolean, tested: Boolean } ``` -------------------------------- ### AStarFinder with Diagonal Movement and Corner Crossing Prevention Source: https://github.com/qiao/pathfinding.js/blob/master/README.md Configures the AStarFinder to allow diagonal movement and prevent paths from crossing corners of occupied grid blocks. This is useful for entities with physical width. ```javascript var finder = new PF.AStarFinder({ allowDiagonal: true, dontCrossCorners: true }); ``` -------------------------------- ### Disable Diagonal Movement Source: https://github.com/qiao/pathfinding.js/blob/master/docs/user-guide/diagonal-movement.md Configures the AStarFinder to never allow diagonal movement by setting the 'diagonalMovement' option to PF.DiagonalMovement.Never. This ensures that paths found will only be straight. ```javascript var finder = new PF.AStarFinder({ diagonalMovement: PF.DiagonalMovement.Never }); ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.