Created
November 3, 2023 03:22
-
-
Save mpr0xy/617f75332fa79b127d6b01f74b75f298 to your computer and use it in GitHub Desktop.
矩阵旋转,可用在俄罗斯方块变换方块形状上
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
const rotate = function (matrix, direction) { | |
for (let y = 0; y < matrix.length; ++y) { | |
for (let x = 0; x < y; ++x) { | |
[matrix[x][y], matrix[y][x]] = [matrix[y][x], matrix[x][y]]; | |
} | |
} | |
if (direction === 'clockWise') { | |
// 顺时针旋转 | |
matrix.forEach((row) => row.reverse()); | |
} else { | |
// 逆时针旋转 | |
matrix.reverse(); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment