DashControl
DashControl

Reputation: 284

Rotate NxN Matrix Counter(anti)-Clockwise 90 Degress

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

Answers (5)

Abhishek katiyar
Abhishek katiyar

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

  1. Create a matrix , in above code It is 3*3 matrix
  2. Creating 3 stacks from each row of 3*3 matrix
  3. Pop from each stack one by one in parallel and and re-creating a matrix.
  4. Printing the new tilted matrix by 90 degree(Anticlockwise).

*/

Upvotes: 0

taocp
taocp

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

fatihk
fatihk

Reputation: 7929

You can just take the transpose 3 times, if you are using a particular matrix library

Upvotes: 0

Pyrce
Pyrce

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

Brian Wren
Brian Wren

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

Related Questions