Reputation: 18612
I need to implement two methods that work with 2D arrays:
grayAndFlipLeftToRight
- This method converts the image into a 2D array of gray scale values and then flips it left to right. That is, an object that was facing left will now be facing right. The elements on the left most column will be swapped with those in the right-most column and so on.
grayAndRotate90
- This method converts the image into a 2D array of gray scale values and then rotates it 90 degrees clockwise (lays it on its right side).
Code:
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);
}
}
My implementation doesn't work correctly for both methods.
Upvotes: 1
Views: 4273
Reputation: 11
Interestingly, if you want to rotate counter-clockwise 90 the code looks like this:
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);
}
Upvotes: 1
Reputation: 3154
I think you messed up with the looping a bit. This should get your work done:
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);
}
A blunder in your code is the line(in rotate method) :
pixels[i][j] = rotate[seek][numberOfColums - 1];
You were modifying contents of the input array itself and doing nothing with rotate, the output.
Upvotes: 2