You are viewing a single comment's thread. Return to all comments →
Javascript
function matrixRotation(matrix, r) { // Write your code here const rows = matrix.length; const cols = matrix[0].length; const buffer = new Array((rows + cols - 2) * 2); const m_action_walk = (i, s, bs, mx, my) => { buffer[(i + s) % bs] = matrix[my][mx]; }; const m_action_write = (i, s, bs, mx, my) => { matrix[my][mx] = buffer[(i + s) % bs]; }; const walk_matrix_around = (x, y, action, s) => { let i = 0; let bsize = (rows - 2 * x + cols - 2 * y) * 2 - 4; for (let row = y; row < rows - y; row++) action(i++, s, bsize, x, row); for (let col = x + 1; col < cols - x; col++) action(i++, s, bsize, col, rows - y - 1); for (let row = rows - y - 2; row >= y; row--) action(i++, s, bsize, cols - x - 1, row); for (let col = cols - x - 2; col > x; col--) action(i++, s, bsize, col, y); }; for(let xy = 0; xy < rows - xy && xy < cols - xy; xy++) { walk_matrix_around(xy, xy, m_action_walk, r); walk_matrix_around(xy, xy, m_action_write, 0); } for (let row = 0; row < rows; row++) { console.log(matrix[row].join(' ')); } }
Seems like cookies are disabled on this browser, please enable them to open this website
Matrix Layer Rotation
You are viewing a single comment's thread. Return to all comments →
Javascript