Skip to content

Instantly share code, notes, and snippets.

@jjzazuet
Created February 25, 2019 18:42
Show Gist options
  • Save jjzazuet/3168bcbea146b22bfe8227c55604a402 to your computer and use it in GitHub Desktop.
Save jjzazuet/3168bcbea146b22bfe8227c55604a402 to your computer and use it in GitHub Desktop.
Rotate double matrix clock-wise or counter-clock-wise
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