Answers for "rotate a 2d matrix by 90 degrees"

3

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)
Posted by: Guest on September-13-2020
0

rotate 2d array

#clockwise:
rotated = list(zip(*original[::-1]))
#counterclockwise:
rotated_ccw = list(zip(*original))[::-1]
Posted by: Guest on July-26-2021
0

rotate 2d array

// For a nxn Matrix
//  1 2 3      7 4 1
//  4 5 6  ->  8 5 2
//  7 8 9      9 6 3

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;
}
Posted by: Guest on February-25-2021

Code answers related to "rotate a 2d matrix by 90 degrees"

Browse Popular Code Answers by Language