3

I'm trying to rotate a matrix counterclockwise by 90 degrees in Java. I found answers on how to do this with a 2D matrix, but my matrix is 3D.

Here's how I found out on how to do a 2D rotation:

static int[][] rotateCW(int[][] mat) {
    final int M = mat.length;
    final int N = mat[0].length;
    int[][] ret = new int[N][M];
    for (int r = 0; r < M; r++) {
        for (int c = 0; c < N; c++) {
            ret[c][M-1-r] = mat[r][c];
        }
    }
    return ret;
}

How would I go about rotating a 3D matrix then?

ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
NewGradDev
  • 1,004
  • 6
  • 16
  • 35

1 Answers1

11

By multiplying your matrix with a rotation matrix

The basic matrix for the x-axis is:

        | 1     0      0    |
Rx(a) = | 0  cos(a) -sin(a) |
        | 0  sin(a)  cos(a) |

For 90 degrees simply set cos(90) = 0 and sin(90) = 1 which should lead to:

        | 1     0      0    |
Rx(a) = | 0     0     -1    |
        | 0     1      0    |
P.J.Meisch
  • 18,013
  • 6
  • 50
  • 66
stacker
  • 68,052
  • 28
  • 140
  • 210
  • http://stackoverflow.com/questions/14310347/gyroscope-issues-with-device-orientation/14311815#comment19887144_14311815 This was my original problem thread. The accepted answer on that page combined with your answer gave me the hints that i need to solve my issue. Thanks for all your help! :D – NewGradDev Jan 16 '13 at 21:54