|
| 1 | +## Documentation |
| 2 | + |
| 3 | +### Constants |
| 4 | +Vertical and Horizontal cost: 10 |
| 5 | +Diagonal cost: 14 |
| 6 | + |
| 7 | +### Heuristics |
| 8 | + To find the h value, there are 4 possible heuristics contemplated: |
| 9 | +- **Manhattan distance:** estimate the distance considering only horizontal-vertical movements. Ignores obstacles |
| 10 | +- **Diagonal distance:** estimate the distance considering all the 8 possible directions. Ignores obstacles |
| 11 | +- **Euclid distance:** estimate the distance as the crow flies. Ignores obstacles |
| 12 | +- **Dijkstra (h = 0):** always set h = 0. It behaves exactly like Dijkstra's algorithm |
| 13 | + |
| 14 | +### main.js |
| 15 | +```javascript |
| 16 | +/** |
| 17 | +* Called at the start |
| 18 | +*/ |
| 19 | +function setup() |
| 20 | + |
| 21 | +/** |
| 22 | +* Called each frame |
| 23 | +*/ |
| 24 | +function draw() |
| 25 | + |
| 26 | +/** |
| 27 | +* Set up the buttons variables and set the value for each button in settings |
| 28 | +*/ |
| 29 | +function setupButtons() |
| 30 | + |
| 31 | +/** |
| 32 | +* Initialize the squares matrix so that everyghing is walkable |
| 33 | +*/ |
| 34 | +function initializeMatrix() |
| 35 | + |
| 36 | +/** |
| 37 | +* Udate the matrix with the new width and height but trying to keep the elements that don't need deleting |
| 38 | +*/ |
| 39 | +function updateMatrix() |
| 40 | +/** |
| 41 | +* Draw the square matrix on the canvas, according to the state of each square |
| 42 | +*/ |
| 43 | +function drawMatrix() |
| 44 | + |
| 45 | +/** |
| 46 | +* Draw both end and start positions |
| 47 | +*/ |
| 48 | +function drawStartEnd() |
| 49 | + |
| 50 | +/** |
| 51 | +* Make the selected palette follow the mouse |
| 52 | +*/ |
| 53 | +function drawCurrentMouse() |
| 54 | + |
| 55 | +/** |
| 56 | +* If the position is valid, apply the correct palette on the square |
| 57 | +*/ |
| 58 | +function mousePressed() |
| 59 | + |
| 60 | +/** |
| 61 | +* If the mouse is locked, keep drawing the current palette |
| 62 | +*/ |
| 63 | +function mouseDragged() |
| 64 | + |
| 65 | +/** |
| 66 | +* Unlock the mouse |
| 67 | +*/ |
| 68 | +function mouseReleased() |
| 69 | + |
| 70 | +/** |
| 71 | +* Check if the given vecor is inside the matrix borders |
| 72 | +* @param {p5.Vector} v - Vetor to check |
| 73 | +* @returns {boolean} Whether or not the vector is valid |
| 74 | +*/ |
| 75 | + |
| 76 | +function validVector(v) |
| 77 | + |
| 78 | +/** |
| 79 | +* Set the start position and checks for any conflicts |
| 80 | +* @param {number} x - x coordinate of the start position |
| 81 | +* @param {number} y - y coordinate of the start position |
| 82 | +*/ |
| 83 | +function setStart(x, y) |
| 84 | + |
| 85 | +/** |
| 86 | +* Set the end position and checks for any conflicts |
| 87 | +* @param {number} x - x coordinate of the end position |
| 88 | +* @param {number} y - y coordinate of the end position |
| 89 | +*/ |
| 90 | +function setEnd(x, y) |
| 91 | + |
| 92 | +/** |
| 93 | +* If the start or end position are on this point, remove them |
| 94 | +* @param {number} x - x coordinate of the point |
| 95 | +* @param {number} y - y coordinate of the point |
| 96 | +*/ |
| 97 | +function removeStartEnd(x, y) |
| 98 | + |
| 99 | +/** |
| 100 | +* Start the astar algorithm animation |
| 101 | +*/ |
| 102 | +function startAlgotithm() |
| 103 | + |
| 104 | +/** |
| 105 | +* Clears the board |
| 106 | +*/ |
| 107 | +function clearBoard() |
| 108 | + |
| 109 | +/** |
| 110 | +* Set the palette with the given index {walkable, unwalkable, start, end, special_ground} |
| 111 | +* @param {string} id - id of the wanted palette |
| 112 | +*/ |
| 113 | +function setPalette(id) |
| 114 | + |
| 115 | +/** |
| 116 | +* Apply the changes in the settings |
| 117 | +*/ |
| 118 | +function apply() |
| 119 | + |
| 120 | +/** |
| 121 | +* Cancels every change in the settings |
| 122 | +*/ |
| 123 | +function cancel() |
| 124 | + |
| 125 | +/** |
| 126 | + Set the currentStatus value based of the chosen status |
| 127 | +* @param {string} newStatus - status to use when changing the editor mode value |
| 128 | +*/ |
| 129 | +function setStatus(newStatus) |
| 130 | +``` |
| 131 | +### astar.js |
| 132 | +```javascript |
| 133 | +/** |
| 134 | +* Class that handles the A* algorithm |
| 135 | +*/ |
| 136 | +class Astar |
| 137 | + |
| 138 | +/** |
| 139 | +* Create an istance of Astar |
| 140 | +*/ |
| 141 | +constructor() |
| 142 | + |
| 143 | +/** |
| 144 | +* Start the algorithm animation |
| 145 | +*/ |
| 146 | +startAlgorithm() |
| 147 | + |
| 148 | +/** |
| 149 | +* Clear the point matrix and the openPoints array |
| 150 | +*/ |
| 151 | +clearAll() |
| 152 | + |
| 153 | +/** |
| 154 | +* Inizialize the starting point |
| 155 | +*/ |
| 156 | +initializeAlgorithm() |
| 157 | + |
| 158 | +/** |
| 159 | +* Follows the Astar algorithm step by step. Called each frame |
| 160 | +*/ |
| 161 | +stepSolution() |
| 162 | + |
| 163 | +/** |
| 164 | +* Check all the neighbours of the point and, if they are valid points, calculate their f value |
| 165 | +* @param {number} x - x coordinate of the point |
| 166 | +* @param {number} y - y coordinate of the point |
| 167 | +*/ |
| 168 | +checkNeighbours(x, y) |
| 169 | + |
| 170 | +/** |
| 171 | +* Calculate the new F value for the point, if the g provided is less than the current g |
| 172 | +* @param {number} x - x coordinate of the point |
| 173 | +* @param {number} y - y coordinate of the point |
| 174 | +* @param {number} g - new g value calculated for the point |
| 175 | +*/ |
| 176 | +calculateF(x, y, g, px, py) |
| 177 | + |
| 178 | +/** |
| 179 | +* Calculate the heuristic distance from the indicated point to the end point |
| 180 | +* @param {number} x - x coordinate of the point |
| 181 | +* @param {number} y - y coordinate of the point |
| 182 | +* @returns {number} The value h |
| 183 | +*/ |
| 184 | +calculateH(x, y) |
| 185 | + |
| 186 | +/** |
| 187 | +* Retrace the path discovered by the Astar algorithm and color it |
| 188 | +*/ |
| 189 | +retracePath() |
| 190 | + |
| 191 | +/** |
| 192 | +* Retrace the all the path tried by the Astar algorithm and color it |
| 193 | +*/ |
| 194 | +retraceFailPath() |
| 195 | + |
| 196 | +/** |
| 197 | +* Generate a string containing all the point's info |
| 198 | +* @param {number} x - x coordinate of the point |
| 199 | +* @param {number} y - y coordinate of the point |
| 200 | +* @returns {string} - Point's info: h, g and f values |
| 201 | +*/ |
| 202 | +getPointInfo(x, y) |
| 203 | + |
| 204 | +/** |
| 205 | +* Draw some text on the chosen square |
| 206 | +* @param {number} x - x coordinate of the square |
| 207 | +* @param {number} y - y coordinate of the square |
| 208 | +* @param {boolean} override - whether or not the intern of the square should be recolored |
| 209 | +*/ |
| 210 | +drawText(x, y, override) |
| 211 | + |
| 212 | + |
| 213 | +/** |
| 214 | +* Draw a stroke around the chosen square |
| 215 | +* @param {number} x - x coordinate of the square |
| 216 | +* @param {number} y - y coordinate of the square |
| 217 | +* @param {p5.color} color - color of the stroke |
| 218 | +*/ |
| 219 | +drawFocus(x, y, color) |
| 220 | + |
| 221 | +/** |
| 222 | +* Push the point into the openPoints array, if it is not present yet |
| 223 | +* @param {any} point - point to push in the array |
| 224 | +*/ |
| 225 | +pushToOpenNodes(point) |
| 226 | +``` |
0 commit comments