Skip to content

Instantly share code, notes, and snippets.

@new-guy
Created August 25, 2015 21:28
Show Gist options
  • Save new-guy/d4d8e9cfeb47e358f62b to your computer and use it in GitHub Desktop.
Save new-guy/d4d8e9cfeb47e358f62b to your computer and use it in GitHub Desktop.
exports.reversePath = function(path)
{
var reversedPath = [];
for(var i = 0; i < path.length; i++)
{
var currentStep = path[i];
var dx = currentStep.dx * -1;
var dy = currentStep.dy * -1;
var x = currentStep.x - currentStep.dx;
var y = currentStep.y - currentStep.dy;
var direction = ((currentStep.direction + 4) % 8);
if(direction === 0) direction = 8;
reversedPath.unshift({x: x, y: y, dx: dx, dy: dy, direction: direction});
}
return reversedPath;
}
@findoff
Copy link

findoff commented Aug 25, 2015

exports.reversePath = function(path)
{
    var reversedPath = [];

    for(var currentStep of path){ // ES6 supported

        var dx = currentStep.dx * -1;
        var dy = currentStep.dy * -1;

        var x = currentStep.x - currentStep.dx;
        var y = currentStep.y - currentStep.dy;

        var direction = ((currentStep.direction + 3) % 8) + 1; // substruct 1 from 4 and add 1 after to shift start by 1, to prevent if

        reversedPath.unshift({x: x, y: y, dx: dx, dy: dy, direction: direction});
    }

    return reversedPath;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment