Last active
November 5, 2022 13:37
-
-
Save kyle-west/9b8f0ae6b79c324c10e2d2346f8868b5 to your computer and use it in GitHub Desktop.
Rotate (right or left) a 2D matrix implemented as a 1D array. Also it should be noted that this is called "transpose" in the math world.
This file contains hidden or 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
// You can adjust these params to suit your actual dimensions | |
const width = 10; | |
const height = 10; | |
const idx = (y, x) => y * height + x | |
// GIVEN: | |
// const matrix1D = [0,1,2,3, /* ... */, 98,99] | |
// const matrix2D = [ | |
// [0, 1, 2, 3, 4, 5, 6, 7, 8, 9], | |
// // ... | |
// [90, 91, 92, 93, 94, 95, 96, 97, 98, 99], | |
// ]; | |
// | |
// THEN: | |
// matrix1D[idx(2,3)] === matrix2D[2][3] | |
// ================================================================== | |
function rotateOverDiagonal(matrix) { | |
const copy = [...matrix] | |
for(let x = 0; x < height; ++x) { | |
for(let y = 0; y < x; ++y) { | |
let temp = copy[idx(x,y)]; | |
copy[idx(x,y)] = copy[idx(y,x)]; | |
copy[idx(y,x)] = temp; | |
} | |
} | |
return copy | |
} | |
function rotateRight (matrix) { | |
const copy = rotateOverDiagonal(matrix) | |
// rotate over the column center clockwise | |
for (let x = 0; x < width; ++x) { | |
for (let y = 0; y < height / 2; ++y) { | |
const temp = copy[idx(x,y)]; | |
copy[idx(x,y)] = copy[idx(x,width - y - 1)]; | |
copy[idx(x,width - y - 1)] = temp; | |
} | |
} | |
return copy | |
} | |
function rotateLeft (matrix) { | |
const copy = rotateOverDiagonal(matrix) | |
// rotate over the column center counter-clockwise | |
for (let x = 0; x < width; ++x) { | |
for (let y = 0; y < height / 2; ++y) { | |
const temp = copy[idx(x,y)]; | |
copy[idx(x,y)] = copy[idx(x,(width + y) % width)]; | |
copy[idx(x,(width + y) % width)] = temp; | |
} | |
} | |
return copy | |
} | |
// ================================================================== | |
console.log(rotateRight([ | |
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, | |
10, 11, 12, 13, 14, 15, 16, 17, 18, 19, | |
20, 21, 22, 23, 24, 25, 26, 27, 28, 29, | |
30, 31, 32, 33, 34, 35, 36, 37, 38, 39, | |
40, 41, 42, 43, 44, 45, 46, 47, 48, 49, | |
50, 51, 52, 53, 54, 55, 56, 57, 58, 59, | |
60, 61, 62, 63, 64, 65, 66, 67, 68, 69, | |
70, 71, 72, 73, 74, 75, 76, 77, 78, 79, | |
80, 81, 82, 83, 84, 85, 86, 87, 88, 89, | |
90, 91, 92, 93, 94, 95, 96, 97, 98, 99, | |
])) // --> | |
// [ | |
// 90, 80, 70, 60, 50, 40, 30, 20, 10, 0, | |
// 91, 81, 71, 61, 51, 41, 31, 21, 11, 1, | |
// 92, 82, 72, 62, 52, 42, 32, 22, 12, 2, | |
// 93, 83, 73, 63, 53, 43, 33, 23, 13, 3, | |
// 94, 84, 74, 64, 54, 44, 34, 24, 14, 4, | |
// 95, 85, 75, 65, 55, 45, 35, 25, 15, 5, | |
// 96, 86, 76, 66, 56, 46, 36, 26, 16, 6, | |
// 97, 87, 77, 67, 57, 47, 37, 27, 17, 7, | |
// 98, 88, 78, 68, 58, 48, 38, 28, 18, 8, | |
// 99, 89, 79, 69, 59, 49, 39, 29, 19, 9 | |
// ] | |
console.log(rotateLeft([ | |
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, | |
10, 11, 12, 13, 14, 15, 16, 17, 18, 19, | |
20, 21, 22, 23, 24, 25, 26, 27, 28, 29, | |
30, 31, 32, 33, 34, 35, 36, 37, 38, 39, | |
40, 41, 42, 43, 44, 45, 46, 47, 48, 49, | |
50, 51, 52, 53, 54, 55, 56, 57, 58, 59, | |
60, 61, 62, 63, 64, 65, 66, 67, 68, 69, | |
70, 71, 72, 73, 74, 75, 76, 77, 78, 79, | |
80, 81, 82, 83, 84, 85, 86, 87, 88, 89, | |
90, 91, 92, 93, 94, 95, 96, 97, 98, 99, | |
])) // --> | |
// [ | |
// 0, 10, 20, 30, 40, 50, 60, 70, 80, 90, | |
// 1, 11, 21, 31, 41, 51, 61, 71, 81, 91, | |
// 2, 12, 22, 32, 42, 52, 62, 72, 82, 92, | |
// 3, 13, 23, 33, 43, 53, 63, 73, 83, 93, | |
// 4, 14, 24, 34, 44, 54, 64, 74, 84, 94, | |
// 5, 15, 25, 35, 45, 55, 65, 75, 85, 95, | |
// 6, 16, 26, 36, 46, 56, 66, 76, 86, 96, | |
// 7, 17, 27, 37, 47, 57, 67, 77, 87, 97, | |
// 8, 18, 28, 38, 48, 58, 68, 78, 88, 98, | |
// 9, 19, 29, 39, 49, 59, 69, 79, 89, 99 | |
// ] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@drewswaycool let me know if you have any questions. If you wanted in-place swapping just replace the
copy
vars formatrix
in the functions.