Skip to content

Instantly share code, notes, and snippets.

@vancouverwill
Last active August 29, 2015 14:23
Show Gist options
  • Select an option

  • Save vancouverwill/e412b2b2bd0069c950c0 to your computer and use it in GitHub Desktop.

Select an option

Save vancouverwill/e412b2b2bd0069c950c0 to your computer and use it in GitHub Desktop.
determine how far from the centre an index is given that the array has been rotated an unknown amount and we know the middleIndex
displacementFromMiddle = function(index, arraySize, middleIndex) {
if (index == middleIndex) return 0;
var imagesRadius = Math.floor(arraySize / 2)
var x = index - middleIndex;
var y;
if (x > imagesRadius) {
y = x - arraySize;
}
else if (x < -imagesRadius) {
y = x + arraySize
}
else {
y = x;
}
return y;
}
var result = displacementFromMiddle(2, 5, 0)
var result = displacementFromMiddle(3, 5, 0)
var result = displacementFromMiddle(4, 5, 0)
var result = displacementFromMiddle(5, 5, 0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment