我需要实现两种适用于二维数组的方法:
grayAndFlipLeftToRight
- 此方法将图像转换为灰度值的二维数组,然后将其从左向右翻转。也就是说,一个面向左侧的对象现在将面向右侧。最左列的元素将与最右列的元素交换,依此类推。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);
}
}
我的实现对于这两种方法都不能正常工作。
- 如何解决这个问题?