patternjavaMinor
Rotate an image by 90 degrees
Viewed 0 times
rotateimagedegrees
Problem
Looking for review, good practices, optimizations, clean code tips etc.
```
final class Pixel {
private final int color;
public Pixel(int color) {
this.color = color;
}
public int getColor() {
return color;
}
}
/**
* Rotates an image by ninety degrees.
*
* Complexity:
O(row col)
*/
public final class Rotate {
private Rotate() { }
/**
* Rotes the image in anti clockwise direction.
*
* @param image the image to be rotated.
* @return the titled image.
* @throws NPE
*/
public static Pixel[][] rotateAntiClockWise(Pixel[][] image) {
int col = image.length;
int row = image[0].length;
final Pixel[][] rotatedPixel = new Pixel[row][col];
for (int i = 0; i < image.length; i++) {
for (int j = 0; j < image[0].length; j++) {
rotatedPixel[image[0].length -1 -j][i] = image[i][j];
}
}
return rotatedPixel;
}
/**
* Rotates the image in clockwise direction.
*
* @param image the image to be rotated.
* @returns the rotated image.
* @throws NPE
*/
public static Pixel[][] rotateClockWise(Pixel[][] image) {
int col = image.length;
int row = image[0].length;
final Pixel[][] rotatedPixel = new Pixel[row][col];
for (int i = 0; i < image.length; i++) {
for (int j = 0; j < image[0].length; j++) {
rotatedPixel[j][image.length -1 - i] = image[i][j];
}
}
return rotatedPixel;
}
public static void main(String[] args) {
// int[][] m = { {1, 2, 3, 4} ,
// {10, 20, 30, 40}};
Pixel[][] image = new Pixel[2][4];
image[0][0] = new Pixel(1);
image[0][1] = new Pixel(2);
image[0][2] = new Pixel(3);
image[0][3] = new Pixel(4);
image[1][0] = new Pixel(10);
image[1][1] = new Pixel(20);
image[1][2] = new Pixel
```
final class Pixel {
private final int color;
public Pixel(int color) {
this.color = color;
}
public int getColor() {
return color;
}
}
/**
* Rotates an image by ninety degrees.
*
* Complexity:
O(row col)
*/
public final class Rotate {
private Rotate() { }
/**
* Rotes the image in anti clockwise direction.
*
* @param image the image to be rotated.
* @return the titled image.
* @throws NPE
*/
public static Pixel[][] rotateAntiClockWise(Pixel[][] image) {
int col = image.length;
int row = image[0].length;
final Pixel[][] rotatedPixel = new Pixel[row][col];
for (int i = 0; i < image.length; i++) {
for (int j = 0; j < image[0].length; j++) {
rotatedPixel[image[0].length -1 -j][i] = image[i][j];
}
}
return rotatedPixel;
}
/**
* Rotates the image in clockwise direction.
*
* @param image the image to be rotated.
* @returns the rotated image.
* @throws NPE
*/
public static Pixel[][] rotateClockWise(Pixel[][] image) {
int col = image.length;
int row = image[0].length;
final Pixel[][] rotatedPixel = new Pixel[row][col];
for (int i = 0; i < image.length; i++) {
for (int j = 0; j < image[0].length; j++) {
rotatedPixel[j][image.length -1 - i] = image[i][j];
}
}
return rotatedPixel;
}
public static void main(String[] args) {
// int[][] m = { {1, 2, 3, 4} ,
// {10, 20, 30, 40}};
Pixel[][] image = new Pixel[2][4];
image[0][0] = new Pixel(1);
image[0][1] = new Pixel(2);
image[0][2] = new Pixel(3);
image[0][3] = new Pixel(4);
image[1][0] = new Pixel(10);
image[1][1] = new Pixel(20);
image[1][2] = new Pixel
Solution
Assuming that you will want to do more interesting things with these images later, you should probably come up with a better OOP design. I suggest:
It's practically no work to rearrange the code that way. In return, you gain the flexibility to do things like:
public interface ImageTransformation {
public Pixel[][] transform(Pixel[][] image);
}
public class Clockwise90Rotation implements ImageTransformation {
...
}
public class AntiClockwise90Rotation implements ImageTransformation {
...
}It's practically no work to rearrange the code that way. In return, you gain the flexibility to do things like:
ImageTransformation[] pipeline = new ImageTransformation[] {
new Clockwise90Rotation(),
new ContrastEnhancer(),
new GaussianBlur(/* radius= */ 5)
};
for (ImageTransformation t : pipeline) {
image = t.transform(image);
}Code Snippets
public interface ImageTransformation {
public Pixel[][] transform(Pixel[][] image);
}
public class Clockwise90Rotation implements ImageTransformation {
...
}
public class AntiClockwise90Rotation implements ImageTransformation {
...
}ImageTransformation[] pipeline = new ImageTransformation[] {
new Clockwise90Rotation(),
new ContrastEnhancer(),
new GaussianBlur(/* radius= */ 5)
};
for (ImageTransformation t : pipeline) {
image = t.transform(image);
}Context
StackExchange Code Review Q#39265, answer score: 6
Revisions (0)
No revisions yet.