How to rotate a 3D matrix 90 degrees counterclockwise?

I am trying to rotate a matrix counterclockwise 90 degrees in Java. I found answers to the question of how to do this using a 2D matrix, but my matrix is ​​3D.

Here's how I found out how to do two-dimensional 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 do I rotate a rotating three-dimensional matrix?

+1
source share
1 answer

Multiplying your matrix with a rotation matrix

The base matrix for the x axis:

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

For 90 degrees, just set cos (90) = 0 and sin (90) = 1, which should result in:

        | 1     0      0    |
Rx(a) = | 0     0     -1    |
        | 0     1      0    |
+5
source

All Articles