1.6 图像旋转

来源:互联网 发布:数控系统模拟软件 编辑:程序博客网 时间:2024/06/09 13:41

Given an image represented by an NxN matrix, where each pixel in the image is
4 bytes, write a method to rotate the image by 90 degrees. Can you do this in
place?

package test;



public class JumpTwo {
 
    public static void main(String[] args) {
    
        int[][] matr = {{1,2,3},{4,5,6},{7,8,9}};
        int n=3, m=3;
        for(int i=0;i<n;i++){
            for(int j=0;j<m;j++){
                System.out.print("\t"+ matr[i][j]);
            }
            System.out.println();
        }
        System.out.println("after rotation");
        
         rotate(matr,n);
        for(int i=0;i<n;i++){
            for(int j=0;j<m;j++){
                System.out.print("\t"+ matr[i][j]);
            }
            System.out.println();
        }
    }


    public static void rotate(int[][] s, int n) {
        for(int i=0;i<n;i++){
            for(int j=i;j<n-i-1;j++){             错了好久才搞出来,还是比较困惑
                int tmp=s[i][j];
                s[i][j] = s[n-j-1][i];
                s[n-j-1][i]= s[n-i-1][n-j-1];
                s[n-i-1][n-j-1] = s[j][n-1-i];
                s[j][n-1-i]=tmp;
                
            }
            
        }


    }
}
    

0 0