2013-07-30 109 views
0

我需要实现两种方法,用二维数组工作:swaping和旋转2D阵列

  • grayAndFlipLeftToRight - 该方法将图像转换成灰度值的2D阵列,然后将其翻转左到右。也就是说,面向左的对象现在将面向右。最左列的元素将与最右列的元素交换,依此类推。

  • grayAndRotate90 - 此方法将图像转换为二维灰度值数组,然后顺时针旋转90度(将其放置在右侧)。

代码:

public class PictureUtil 
{ 
    /** 
    * Gets a version of the given Picture in gray scale and flipped left to right 
    * @param pic the Picture to convert to gray scale and flip 
    * @return a version of the original Picture in gray scale and flipped 
    * left to right. 
    */ 
    public static Picture grayAndFlipLeftToRight(Picture pic) 
    { 
     int[][] pixels = pic.getGrayLevels(); 

     for (int i = 0; i < pixels.length; i++) {    
      for (int fromStart = 0; fromStart < pixels[0].length; fromStart++) { 
       int fromEnd = pixels[0].length-1; 

       while (fromEnd >= 0) { 
        int saved = pixels[i][fromStart]; 
        pixels[i][fromStart] = pixels[i][fromEnd]; 
        pixels[i][fromEnd] = saved; 

        fromEnd--; 
       }            
      } 
     } 

     return new Picture(pixels); 
    } 

    /** 
    * Gets a version of the given Picture in gray scale and rotated 90 degrees clockwise 
    * @param pic the Picture to convert to gray scale and rotate 90 degrees clockwise 
    * @return a version of the original Picture in gray scale and rotated 90 degrees clockwise 
    */ 
    public static Picture grayAndRotate90(Picture pic) 
    { 
     int[][] pixels = pic.getGrayLevels(); 

     int numberOfColums = pixels.length; 
     int numberOfRows = pixels[0].length;   
     int[][] rotate = new int[numberOfRows][numberOfColums]; 

     for (int i = 0; i < pixels.length; i++) { 
      int seek = 0; 
      for (int j = 0; j < pixels[0].length; j++) {    
       pixels[i][j] = rotate[seek][numberOfColums - 1]; 
       seek++; 
      } 
     } 

     return new Picture(rotate); 
    } 
} 

我的实现不正确两种方法的工作。

  • 如何解决这个问题?

回答

1

我觉得你搞砸了循环了一下。这应该让您的工作:

public static Picture grayAndFlipLeftToRight(Picture pic) 
{ 
    int[][] pixels = pic.getGrayLevels(); 

    for (int i = 0; i < pixels.length; i++) { 
     for (int curr = 0; curr < (pixels[0].length + 1)/2; curr++) { 

      int saved = pixels[i][curr]; 
      pixels[i][curr] = pixels[i][pixels[0].length - 1 - curr]; 
      pixels[i][pixels[0].length - 1 - curr] = saved; 
     } 
    } 

    return new Picture(pixels); 
} 

public static Picture grayAndRotate90(Picture pic) 
{ 
    int[][] pixels = pic.getGrayLevels(); 

    int[][] rotate = new int[pixels[0].length][pixels.length]; 

    for (int i = 0; i < pixels[0].length; i++) { 
     for (int j = 0; j < pixels.length; j++) { 
      rotate[i][pixels.length - 1 - j] = pixels[j][i]; 
     } 
    } 
    return new Picture(rotate); 
} 

代码中的错误是(在旋转法)行:

pixels[i][j] = rotate[seek][numberOfColums - 1]; 

您要修改输入数组本身的内容,并与旋转无所事事输出为

0

有趣的是,如果你想逆时针旋转90的代码如下所示:

public static Picture grayAndRotate90CC(Picture pic) 
{ 
    int[][] pixels = pic.getGrayLevels(); 

    int[][] rotateCC = new int[pixels[0].length][pixels.length]; 

    for (int i = 0; i < pixels[0].length; i++) { 
     for (int j = 0; j < pixels.length; j++) { 
      rotateCC[i][j] = pixels[j][i]; //rotates 90 counter-clockwise! 
     } 
    } 
    return new Picture(rotateCC); 
}