2013-07-30 3 views
0

나는 2 차원 배열 작업을 두 가지 방법을 구현해야합니다swaping 및 회전 2D 배열은

  • grayAndFlipLeftToRight -이 방법은 그레이 스케일 값의 2 차원 배열로 이미지를 변환을 한 다음 왼쪽에서 오른쪽으로 뒤집습니다. 즉, 왼쪽을 향한 물체가 오른쪽을 향하게됩니다. 맨 왼쪽 열에있는 요소는 맨 오른쪽 열에있는 요소와 바뀌게됩니다.

  • grayAndRotate90 -이 방법은 이미지를 그레이 스케일 값의 2D 배열로 변환 한 다음 시계 방향으로 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]; 

당신은 입력 배열 자체의 내용을 수정하고 회전으로 아무것도하지 않고 있었다, 출력. 당신이 시계 반대 방향으로 90 회전 할 경우

0

흥미롭게도, 코드는 다음과 같습니다

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); 
}