Last active
August 29, 2015 14:23
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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