나는 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);
}
}
내 구현이 두 가지 방법에 대해 올바르게 작동하지 않습니다.
- 이 문제를 해결하는 방법은 무엇입니까?