Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rotate matrix in place

I'm solving the rotate an NxN matrix in place question.

It seems that my code makes a rotation, but leaves an X over the image.
So I'm guessing it's rotating the edges incorrectly. I'm attaching two images as sample input and output.

enter image description hereenter image description here

What's wrong with my code:

public static void rotateRight(float[][] img){
    for (int i=0; i<N/2; i++){
        for (int j=i; j<N-i; j++){
            int J_COMP = N-j-1; //complement of J
            int LEFT = i;
            int RIGHT = N-i-1;
            int TOP = i;
            int BOTTOM = N-i-1;

            float temp = img[J_COMP][LEFT];
            img[J_COMP][LEFT] = img[BOTTOM][J_COMP];
            img[BOTTOM][J_COMP] = img[j][RIGHT];
            img[j][RIGHT] = img[TOP][j];
            img[TOP][j] = temp;
        }
    }       
}
like image 608
Trevor Avatar asked Dec 25 '13 14:12

Trevor


2 Answers

You are rotating main diagonals twice.

Fix inner loop (see "fix" comment)

package tests.StackOverflow;

public class Question_20773692 {

    private static int N;

    public static void main(String[] args) {

        float[][] img;
        int count;

        N=3;
        count = 0;
        img = new float[N][N];
        for(int i=0; i<N; ++i) {
            for(int j=0; j<N; ++j) {
                img[i][j] = count++;
            }
        }

        printImg(img);

        rotateRight(img);

        printImg(img);

    }

    public static void printImg(float[][] img) {
        for(int j=0; j<N; ++j) {
            System.out.print("-");
        }
        System.out.println();
        for(int i=0; i<N; ++i) {
            for(int j=0; j<N; ++j) {
                System.out.print((int)(img[i][j]));
            }
            System.out.println();
        }
        for(int j=0; j<N; ++j) {
            System.out.print("-");
        }
        System.out.println();   }

    public static void rotateRight(float[][] img){
        for (int i=0; i<N/2; i++){
            for (int j=i; j<N-i; j++){
            //for (int j=i+1; j<N-i; j++){ //fix
                int J_COMP = N-j-1; //complement of J
                int LEFT = i;
                int RIGHT = N-i-1;
                int TOP = i;
                int BOTTOM = N-i-1;

                float temp = img[J_COMP][LEFT];
                img[J_COMP][LEFT] = img[BOTTOM][J_COMP];
                img[BOTTOM][J_COMP] = img[j][RIGHT];
                img[j][RIGHT] = img[TOP][j];
                img[TOP][j] = temp;
            }
        }       
    }
}
like image 111
Suzan Cioc Avatar answered Sep 26 '22 10:09

Suzan Cioc


In-place rotation of matrix.... I guess this should work.

public void rotate(ArrayList<ArrayList<Integer>> a) {
    int n=a.size()/2;
    for(int i=1;i<=n;i++){
        ArrayList<Integer>temp=a.get(i-1);
        a.set(i-1,a.get(a.size()-i));
        a.set(a.size()-i,temp);
    }
    for(int i=0;i<a.size();i++){
        for(int j=i+1;j<a.get(0).size();j++){
            a.get(i).set(j,a.get(i).get(j)^a.get(j).get(i));
            a.get(j).set(i,a.get(i).get(j)^a.get(j).get(i));
            a.get(i).set(j,a.get(i).get(j)^a.get(j).get(i));
    }
}
}
like image 37
Rakesh M.R. Avatar answered Sep 23 '22 10:09

Rakesh M.R.