This is a pretty simple example , however a few points are tricky. I would suggest you should build your own matrix and apply the logic mentioned here (layers, offset , first, last ,swapping etc) using paper and pen in order to understand the basic logic of layers and cyclic rotation in this square matrix.
It is nothing like any rocket science, all you got to recognize are the points where you need to apply the rotation. In our simple algorithm we obtain those relative positions by using a integer offset. You can think of an offset as a radius of the circle. All points (in our case 4 -> top left, top right, bottom left, and bottom right) of a circle are to be rotated in clockwise direction by 90 degrees.
Please follow the following algorithm for a proper understanding of the whole concept.
import java.util.Scanner;
public class FourByte_ImageRotation {
/**
* Image is by NXN matric. Each pixel is 4 bytes (here this info is to
* create terror in the mind of the interviewee), rotate the image by 90
* degrees.
*
* Easiest and the only method I could understand was the one described in
* the book, so here it is ->
*
* Perform roatation in layers.... perform a cyclic swapping on the edges of
* each layer. Rotate the first layer -> outermost edges by doing a complete
* cyclic 4 number positions swap. apply the same concept to other layers
*
* Layers will grow from outermost layer to innermost center (size/2)
*
* @param args
*/
public static void main(String[] args) {
System.out.println("Please enter the size");
Scanner s = new Scanner(System.in);
int size = s.nextInt();
int[][] image = gyle.random.RandomMatrix.generateRandromMatrix(size);
gyle.random.RandomMatrix.printMatrix(image);
rotateMatrix(image, size);
gyle.random.RandomMatrix.printMatrix(image);
}
public static void rotateMatrix(int[][] image, int size) {
if (size < 2)
return;
for (int layer = 0; layer < image.length / 2; layer++) {
/**
*recognize the first and last elements where defines the boundary elements of one layer (one circle) .
**/
int first = layer;
int last = size - 1 - layer;
for (int i = first; i < last; i++) {
int offset = i - first; // radius or offset at which the
// elements are situated in one layer
// say layer 0,1,2 etc
int temp = image[first][i]; // temperory object used for
// swapping
// bottom left-> top left
image[first][i] = image[last - offset][first]; // (column =
// previousrow
// && row =
// last-distance)
// bottom right -> bottom left
image[last - offset][first] = image[last][last - offset]; // (column
// =
// previousrow
// )
// top right -> bottom right
image[last][last - offset] = image[i][last]; // //(column =
// previousrow
// && row =
// first column)
// temp -> top right
image[i][last] = temp;
}
}
}
}
It is nothing like any rocket science, all you got to recognize are the points where you need to apply the rotation. In our simple algorithm we obtain those relative positions by using a integer offset. You can think of an offset as a radius of the circle. All points (in our case 4 -> top left, top right, bottom left, and bottom right) of a circle are to be rotated in clockwise direction by 90 degrees.
Please follow the following algorithm for a proper understanding of the whole concept.
import java.util.Scanner;
public class FourByte_ImageRotation {
/**
* Image is by NXN matric. Each pixel is 4 bytes (here this info is to
* create terror in the mind of the interviewee), rotate the image by 90
* degrees.
*
* Easiest and the only method I could understand was the one described in
* the book, so here it is ->
*
* Perform roatation in layers.... perform a cyclic swapping on the edges of
* each layer. Rotate the first layer -> outermost edges by doing a complete
* cyclic 4 number positions swap. apply the same concept to other layers
*
* Layers will grow from outermost layer to innermost center (size/2)
*
* @param args
*/
public static void main(String[] args) {
System.out.println("Please enter the size");
Scanner s = new Scanner(System.in);
int size = s.nextInt();
int[][] image = gyle.random.RandomMatrix.generateRandromMatrix(size);
gyle.random.RandomMatrix.printMatrix(image);
rotateMatrix(image, size);
gyle.random.RandomMatrix.printMatrix(image);
}
public static void rotateMatrix(int[][] image, int size) {
if (size < 2)
return;
for (int layer = 0; layer < image.length / 2; layer++) {
/**
*recognize the first and last elements where defines the boundary elements of one layer (one circle) .
**/
int first = layer;
int last = size - 1 - layer;
for (int i = first; i < last; i++) {
int offset = i - first; // radius or offset at which the
// elements are situated in one layer
// say layer 0,1,2 etc
int temp = image[first][i]; // temperory object used for
// swapping
// bottom left-> top left
image[first][i] = image[last - offset][first]; // (column =
// previousrow
// && row =
// last-distance)
// bottom right -> bottom left
image[last - offset][first] = image[last][last - offset]; // (column
// =
// previousrow
// )
// top right -> bottom right
image[last][last - offset] = image[i][last]; // //(column =
// previousrow
// && row =
// first column)
// temp -> top right
image[i][last] = temp;
}
}
}
}
No comments:
Post a Comment