javascript 如何在javascript中旋转数组中的矩阵
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15170942/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to rotate a matrix in an array in javascript
提问by methodofaction
(disclosure, I'm mostly math illiterate).
(披露,我主要是数学文盲)。
I have an array in this format:
我有一个这种格式的数组:
var grid = [
[0,0], [0,1], [0,2], [0,3],
[1,0], [1,1], [1,2], [1,3],
[2,0], [2,1], [2,2], [2,3],
[3,0], [3,1], [3,2], [3,3]
];
I need to "rotate" it by 90deg increments, so it's like this:
我需要以 90 度为增量“旋转”它,所以它是这样的:
var grid = [
[3,0], [2,0], [1,0], [0,0],
[3,1], [2,1], [1,1], [0,1],
[3,2], [2,2], [1,2], [0,2],
[3,3], [2,3], [1,3], [0,3]
];
How do I accomplish this in Javascript?
如何在 Javascript 中完成此操作?
回答by lmortenson
Credit goes to this answerfor the actual rotation method.
对于实际轮换方法,归功于此答案。
My method was pretty straightforward. Just determine what the row length was, and then iterate through each item, converting the array index to x/y equivalents and then apply the method used in the linked answer to rotate. Finally I converted the rotated X/Y coordinates back to an array index.
我的方法很简单。只需确定行长度是多少,然后遍历每个项目,将数组索引转换为 x/y 等效项,然后应用链接答案中使用的方法进行旋转。最后,我将旋转后的 X/Y 坐标转换回数组索引。
var grid = [
[0,0], [0,1], [0,2], [0,3],
[1,0], [1,1], [1,2], [1,3],
[2,0], [2,1], [2,2], [2,3],
[3,0], [3,1], [3,2], [3,3]
];
var newGrid = [];
var rowLength = Math.sqrt(grid.length);
newGrid.length = grid.length
for (var i = 0; i < grid.length; i++)
{
//convert to x/y
var x = i % rowLength;
var y = Math.floor(i / rowLength);
//find new x/y
var newX = rowLength - y - 1;
var newY = x;
//convert back to index
var newPosition = newY * rowLength + newX;
newGrid[newPosition] = grid[i];
}
for (var i = 0; i < newGrid.length; i++)
{
console.log(newGrid[i])
}
The output:
输出:
[3, 0] [2, 0] [1, 0] [0, 0]
[3, 1] [2, 1] [1, 1] [0, 1]
[3, 2] [2, 2] [1, 2] [0, 2]
[3, 3] [2, 3] [1, 3] [0, 3]
Fiddlefor the lazy. And a 5x5 grid fiddleto demonstrate that the algorithm works for N grid sizes as long as they are square.
为懒惰的人小提琴。以及一个5x5 网格小提琴,以证明该算法适用于 N 个网格大小,只要它们是方形的。
回答by Aryan Firouzian
These are two function for clockwise and counterclockwise 90-degree rotation:
这是顺时针和逆时针 90 度旋转的两个函数:
function rotateCounterClockwise(a){
var n=a.length;
for (var i=0; i<n/2; i++) {
for (var j=i; j<n-i-1; j++) {
var tmp=a[i][j];
a[i][j]=a[j][n-i-1];
a[j][n-i-1]=a[n-i-1][n-j-1];
a[n-i-1][n-j-1]=a[n-j-1][i];
a[n-j-1][i]=tmp;
}
}
return a;
}
function rotateClockwise(a) {
var n=a.length;
for (var i=0; i<n/2; i++) {
for (var j=i; j<n-i-1; j++) {
var tmp=a[i][j];
a[i][j]=a[n-j-1][i];
a[n-j-1][i]=a[n-i-1][n-j-1];
a[n-i-1][n-j-1]=a[j][n-i-1];
a[j][n-i-1]=tmp;
}
}
return a;
}
回答by Nitin Jadhav
Those looking for Rotating a two dimentional matrix (a more general case) here is how to do it.
那些在这里寻找旋转二维矩阵(更一般的情况)的人是如何做到的。
example: Original Matrix:
示例:原始矩阵:
[
[1,2,3],
[4,5,6],
[7,8,9]
]
Rotated at 90 degrees:
旋转 90 度:
[
[7,4,1]
[8,5,2]
[9,6,3]
]
This is done in following way:
这是通过以下方式完成的:
matrix[0].map((val, index) => matrix.map(row => row[index]).reverse())
回答by methodofaction
I don't really need to deal with indices, since I can copy the values from one place to the other, this simplifies the answer a bit:
我真的不需要处理索引,因为我可以将值从一个地方复制到另一个地方,这稍微简化了答案:
var grid = [
[0,0], [0,1], [0,2], [0,3], [0,4],
[1,0], [1,1], [1,2], [1,3], [1,4],
[2,0], [2,1], [2,2], [2,3], [2,4],
[3,0], [3,1], [3,2], [3,3], [3,4],
[4,0], [4,1], [4,2], [4,3], [4,4]
];
var side = Math.sqrt(grid.length);
var rotate = function(d,i){
return [Math.abs(i % side - side+1), Math.floor(i/side)]
}
grid = grid.map(rotate);
You can see a jsfiddle here: http://jsfiddle.net/KmtPg/
你可以在这里看到一个 jsfiddle:http: //jsfiddle.net/KmtPg/