【问题标题】:Rotate 2D array by alpha degrees将二维数组旋转 alpha 度
【发布时间】:2022-01-07 05:29:52
【问题描述】:

我写了一个带有两个参数的函数:

  1. JPG 图像作为 3D 数组

  2. 由 alpha 给出的旋转度数 我的做法是:

    public static int[][] rotate(int[][] img, double alpha) { 双弧度 = Math.toRadians(alpha); 双罪 = Math.sin(rad); 双 cos = Math.cos(rad);

     int height = img.length;
     int width = img[0].length;
    
     int[][] rotate = new int[height][width];
    
     for(int i = 0; i < height; i++) {
         for(int j = height - i - 1; j < width; j++) {
    
             if(j < height && i < width) {
    
                 double i_new = Math.floor(cos * (img[i].length - i) - sin * (img[j].length - j)) + i;
                 double j_new = Math.floor(sin * (img[i].length - i) + cos * (img[j].length - j)) + j;
    
                 rotate[i][j] = img[(int)j_new][(int)i_new];
             }
         }
     }
     return rotate;
    

    }

在固定索引范围时,输出是黑色图像。我错过了什么?

【问题讨论】:

    标签: java arrays rotation smoothing


    【解决方案1】:

    过了一会儿,我找到了解决办法。

    注意:它没有使用任何特殊的预定义库。

    在矩阵上运行的全局函数:

    public static int[][] rotate(int[][] img, double alpha) {
    
        double rad = Math.toRadians(alpha);                             //construct of the relevant angles
        double sin = Math.sin(rad);
        double cos = Math.cos(rad);
    
        int height = img.length;
        int width = img[0].length;
    
        int[][] rotate = new int[height][width];
    
        int a = height / 2;                                             //we will use the area of a and b to compare coordinates by the formula given
        int b = width / 2;
    
        for (int i = 0; i < height; i++) {
            for (int j = 0; j < width; j++) {
    
                double i_new = Math.floor(cos * (i - a) - sin * (j - b)) + a;       // following the conversion function
                double j_new = Math.floor(sin * (i - a) + cos * (j - b)) + b;
    
                if (i_new >= rotate.length || i_new < 0 || j_new >= rotate[0].length || j_new < rotate[0][0]) { // if out of scope of the conversion --> print none
                    System.out.print("");                                           //mainly cause 'continue' statements are not necessary in java and JS
                } else {
                    rotate[(int) i_new][(int) j_new] = img[i][j];                   //convert
                }
            }
        }
    
        return rotate;
    }
    

    旋转每个二维矩阵的全局函数:

    public static int[][][] rotate_alpha(int[][][] img, double alpha) {
    
        int height = img[0].length;
        int width = img[0][0].length;
    
        int[][][] rotated = new int[3][height][width];
    
        for (int k = 0; k < 3; k++) {
            rotated[k] = rotate(img[k], alpha);
        }
    
        return rotated;
    }
    

    希望这个话题现在得到解决,并且符合干净代码的所有标准。

    【讨论】:

      猜你喜欢
      • 2011-06-01
      • 2013-10-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多