Skip to content

Instantly share code, notes, and snippets.

@chouclee
Created June 17, 2014 16:34
Show Gist options
  • Save chouclee/38071b3c986338e9ee0c to your computer and use it in GitHub Desktop.
Save chouclee/38071b3c986338e9ee0c to your computer and use it in GitHub Desktop.
[CC150][1.5] Given an image represented by an NxN matrix, where each pixel in the image is 4 bytes, write a method to rotate the image by 90 degrees. Can you do this in place?
public class Image {
private int N;
private int[][] img;
public Image(int[][] image) {
if (image == null) throw new NullPointerException();
this.img = image;
if (image.length != image[0].length) throw new IllegalArgumentException();
this.N = image[0].length;
}
//* need total [(N-1) + (N-3) + ...3 + 1]*3 exchange if N is even
// need total [(N-1) + (N-3) + ...4 + 2 + 0]*3 exchange if N is odd
//* time O(N^2)
public void rotate(String s) {
if (s == "ClockWise") {
for (int j = N - 1; j > (N - 1)/2; j--) {
for (int i = (N - 1) - j; i < j; i++ ) {
exch(i, j, j, N - 1 - i); //(dstX, dstY) = (j, N - 1 - i)
exch(i, j, N - 1 - i, N - 1 - j); // (dstX, dstY) = (odl_dstY, N-1-old_dstX)
exch(i, j, N - 1 - j, i); // (dstX, dstY) = (odl_dstY, N-1-old_dstX)
}
}
}
else if (s == "CounterClockWise") {
for (int j = N - 1; j > (N - 1)/2; j--) {
for (int i = (N - 1) - j; i < j; i++ ) {
exch(i, j, N - 1 - j, i);
exch(i, j, N - 1 - i, N - 1 - j);
exch(i, j, j, N - 1 -i);
}
}
}
else throw new UnsupportedOperationException("Invalid operation");
}
public String toString() {
String s = "";
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++)
s += img[i][j] + " ";
s += "\n";
}
return s;
}
//* Helpler function, in-place exchane
private void exch(int srcX, int srcY, int dstX, int dstY) {
img[srcX][srcY] ^= img[dstX][dstY];
img[dstX][dstY] ^= img[srcX][srcY];
img[srcX][srcY] ^= img[dstX][dstY];
}
public static void main(String[] args) {
System.out.println("***********TEST**************");
int[][] testA = {{1}};
Image a = new Image(testA);
System.out.print(a.toString());
a.rotate("ClockWise");
System.out.println("\nClockWise:");
System.out.print(a.toString());;
a.rotate("CounterClockWise");
System.out.println("\nCounterClockWise:");
System.out.print(a.toString());;
System.out.println("***********TEST**************");
int[][] testB = {{1,2},{3,4}};
Image b = new Image(testB);
System.out.print(b.toString());
b.rotate("ClockWise");
System.out.println("\nClockWise:");
System.out.print(b.toString());
b.rotate("CounterClockWise");
System.out.println("\nCounterClockWise:");
System.out.print(b.toString());;
System.out.println("***********TEST**************");
int[][] testC = {{1,2,3},{4,5,6},{7,8,9}};
Image c = new Image(testC);
System.out.print(c.toString());
c.rotate("ClockWise");
System.out.println("\nClockWise:");
System.out.print(c.toString());
c.rotate("CounterClockWise");
System.out.println("\nCounterClockWise:");
System.out.print(c.toString());;
System.out.println("***********TEST**************");
int[][] testD = {{1,2,3,4},{5,6,7,8},{9,10,11,12},{13,14,15,16}};
Image d = new Image(testD);
System.out.print(d.toString());
d.rotate("ClockWise");
System.out.println("\nClockWise:");
System.out.print(d.toString());
d.rotate("CounterClockWise");
System.out.println("\nCounterClockWise:");
System.out.print(d.toString());;
System.out.println("***********TEST**************");
int[][] testE = {{1,2,3,4,5},{6,7,8,9,10},{11,12,13,14,15},{16,17,18,19,20},
{21,22,23,24,25}};
Image e = new Image(testE);
System.out.print(e.toString());
e.rotate("ClockWise");
System.out.println("\nClockWise:");
System.out.print(e.toString());
e.rotate("CounterClockWise");
System.out.println("\nCounterClockWise:");
System.out.print(e.toString());;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment