Reputation: 284
I have a 2D Matrix M[N][N] that I need to rotate counter-clockwise by 90 degrees. I have seen many answers for clockwise rotation but I cannot find counter-clockwise. How similar are the two operations?
Upvotes: 2
Views: 2681
Reputation: 101
public static void main(String[] args) {
int[][] matrix = createAMatrix(3,3);
List<Stack<Integer>> tiltedMatrix = tiltMatrixBy90Now(matrix, 3);
int[][] newMatrix = new int[3][3];
for(int i = 0; i < 3; i ++) {
for(int j = 0; j < 3; j ++) {
newMatrix[i][j] = tiltedMatrix.get(j).pop();
}
}
//print new matrix
for(int i = 0; i < 3; i ++) {
for(int j = 0; j < 3; j ++) {
System.out.print(newMatrix[i][j]+" ");
}
System.out.println();
}
}
private static List<Stack<Integer>> tiltMatrixBy90Now(int[][] matrix , long order) {
List<Stack<Integer>> stackList = new ArrayList<>();
//filling the stack
for(int i = 0; i< order ; i++) {
stackList.add(new Stack<Integer>());
}
for(int i = 0; i < order; i ++) {
for(int j = 0; j < order; j ++) {
stackList.get(i).push(matrix[i][j]);
}
}
return stackList;
}
private static int[][] createAMatrix(final int a, final int b){
int counter = 1;
int[][] matrix = new int[a][b];
Scanner scanner = new Scanner(System.in);
while(counter <= a*b) {
for(int i = 0; i < a; i ++) {
for(int j = 0; j < b; j ++) {
matrix[i][j] = scanner.nextInt();
counter++;
}
}
}
return matrix;
}
/*
Input matrix (3 by 3) 1 2 3 4 5 6 7 8 9
Output matrix (3 by 3): 3 6 9 2 5 8 1 4 7
Code walk through as text explanation
*/
Upvotes: 0
Reputation: 23664
OK. let us say N =2
to be simple:
1 2
3 4
counter-clockwise 90 degree means that it will become:
2 4
1 3
We have the following rules:
1 last column from top to bottom of original matrix becomes
first row of rotated matrix from left to right
2 first column of original matrix becomes last row of rotated matrix
3 same rules apply to other columns of original matrix
You can easily code this out. Another way to do is to first do a transpose on the matrix then reverse the order of all rows.
Upvotes: 0
Reputation: 7929
You can just take the transpose 3 times, if you are using a particular matrix library
Upvotes: 0
Reputation: 8571
If you reverse the order of each individual row and then taken rows in opposite order from a clockwise rotation, you get a count-clockwise rotation.
A B C G D A A D G C F I
D E F -> Clockwise -> H E B -> Reverse -> B E H -> Opposite -> B E H
G H I I F C Rows C F I Ordering A D G
Matrix Counter
Clockwise
Usually it's easier (and more computationally efficient) to do a clockwise rotation rotation on the original matrix in reverse order if you already have a clockwise rotating algorithm available.
1 2 3 9 8 7 3 6 9
4 5 6 -> Reverse -> 6 5 4 -> Clockwise -> 2 5 8
7 8 9 Indices 3 2 1 1 4 7
Matrix Counter
Clockwise
You can also just take 3 clockwise rotations to get to a counter clockwise rotation.
Though in reality it's usually fairly easy to edit the clockwise algorithm to your purposes directly. So I'd only use the above options if you don't care about efficiency and don't want to work through the logic of changing the direction of rotation.
Upvotes: 3
Reputation: 377
From row(max), decrementing, fill in the result rows(incrementing index) with the values of that column, one after the other (incrementing).
So in a 3 x 3, use (using r, c notation like Excel)
(3, 1), (3, 2), (3, 3), (2, 1), (2, 2), (2, 3),
etc.
Upvotes: 0