A Simple Code to Rotate a 3*3 Matrix by 90 Degrees

Hi, Reading the title u may be thinking….What’s the point?

Simple. If you are doing image processing, you may be asked to rotate images. So, if given a N*N matrix which is a matrix of pixels you can use the same settings to extend this program to a N*N matrix.

Idea?
First take TRANSPOSE, then reverse the order of columns. Simple 🙂
Here is the code :

package bakchod;

public class Rotate90 {
private static void ninety(int[][] x){
int i,j;
int len = x.length;
int[][] fin = new int[3][3];
int t=0;
for(i=0;i<len;i++){
for(j=0;j<len;j++){
int temp;
fin[i][j]=x[j][i];
}
}
for(i =0;i=0;j--){
System.out.print(fin[i][j]+"\t");
}
System.out.println("\n");
}
}
public static void main(String[] args){
int[][] mat = new int[3][3];
int l =1;
for(int i =0;i<3;i++){
for(int j =0;j<3;j++){
mat[i][j]=l;
l++;
}
}
for(int i =0;i<3;i++){
for(int j =0;j<3;j++){
System.out.print(mat[i][j]+"\t");
}
System.out.println("\n");
}

System.out.println("\n\n");
ninety(mat);
}

}