Created
February 25, 2019 18:42
-
-
Save jjzazuet/3168bcbea146b22bfe8227c55604a402 to your computer and use it in GitHub Desktop.
Rotate double matrix clock-wise or counter-clock-wise
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static double[][] rotate(double[][] matrix, boolean ccw) { | |
double [][] out = new double[matrix[0].length][]; | |
for (int i = out.length - 1, j = 0; i >= 0 && j < out.length; i--, j++) { | |
double[] x = new double[matrix.length]; | |
out[ccw ? i : j] = x; | |
for (int k = x.length, l = 0; k >= 0 && l < x.length; k--, l++) { | |
if (ccw) { out[i][l] = matrix[l][j]; } | |
else { out[j][l] = matrix[k - 1][j]; } | |
} | |
} | |
return out; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment