Skip to content

Instantly share code, notes, and snippets.

@jamonholmgren
Created July 31, 2020 05:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jamonholmgren/0d49d000966ebbfb98dca2719a253e98 to your computer and use it in GitHub Desktop.
Save jamonholmgren/0d49d000966ebbfb98dca2719a253e98 to your computer and use it in GitHub Desktop.
// did we find the destination?
if (lowestCostNode.x === state.destX && lowestCostNode.y === state.destY) {
// retrace our steps back to the beginning!
function retrace(node, path) {
// did we find the origin? if so, we're done!
if (node.previous === undefined) return [node, ...path]
// not yet ... let's keep retracing our steps
return retrace(node.previous, [ node, ...path ])
}
// kick off the retracing!
return retrace(lowestCostNode, [])
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment