Skip to content

Instantly share code, notes, and snippets.

@mpr0xy
Created November 3, 2023 03:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mpr0xy/617f75332fa79b127d6b01f74b75f298 to your computer and use it in GitHub Desktop.
Save mpr0xy/617f75332fa79b127d6b01f74b75f298 to your computer and use it in GitHub Desktop.
矩阵旋转,可用在俄罗斯方块变换方块形状上
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