rotate 2d array by 90 degrees code example
Example 1: rotate 2d vector by angle
rotate vector (x1, y1) counterclockwise by the given angle
(angle in radians)
newX = oldX * cos(angle) - oldY * sin(angle)
newY = oldX * sin(angle) + oldY * cos(angle)
Example 2: rotate 2d array
function rotadeMatrix( matrix ) {
var l = matrix.length-1;
for ( let x = 0; x < matrix.length / 2; x++ ) {
for ( let y = x; y < l-x; y++ ) {
let temp = matrix[l-y][x ];
matrix[l-y][x ] = matrix[l-x][l-y];
matrix[l-x][l-y] = matrix[y ][l-x];
matrix[y ][l-x] = matrix[x ][y ];
matrix[x ][y ] = temp;
}
}
return matrix;
}