package exercises;
public class SquareMatrix {
private int[][] matrix;
private int size;
private int row;
private int column;
public SquareMatrix(int size) {
this.size = size;
this.matrix = new int[size][size];
}
public SquareMatrix row(int row) {
this.row = row;
return this;
}
public SquareMatrix column(int column) {
this.column = column;
return this;
}
public void add(int item) {
this.matrix[this.row - 1][this.column - 1] = item;
}
public void rotatePlus90_easy() {
int[][] rotated = new int[size][size];
for(int i = 0; i < size; i++) {
for(int j = 0; j < size; j++) {
rotated[i][j] = matrix[size - j - 1][i];
}
}
this.matrix = rotated;
}
public void rotateMinus90_easy() {
int[][] rotated = new int[size][size];
for(int i = 0; i < size; i++) {
for(int j = 0; j < size; j++) {
rotated[i][j] = matrix[j][size - i - 1];
}
}
this.matrix = rotated;
}
/**
* Imagine matrix as nested squares. Outer square contains a inner, which in turn contains another and so on.
* Do below three steps for each square, start from Outer
* first rotate corners(clockwise)
* then rotate 2nd element in the square
* then rotate 3rd element and goes on.
* i = number of squares(3*3 is 1, 4*4 is 2, 5*5 is 2, 6*6 is 3). For odd size, inner most square is only one element. so it never moves
* j = for a square, move all elements. so it number of elements to be moved on a side
* First index should deal with 'j', if moving rows, else 'i' if moving columns.
* for ex(run and see 5*5 matrix), Top Right Corner 05 to 25 is rows, so use j. But 25 to 21 about columns, so use i.
*/
public void rotate90() {
for(int i = 0; i < size / 2; i++) {
for(int j = i; j < size - i - 1; j++) {
int temp = this.matrix[i][j];
this.matrix[i][j] = this.matrix[size - j - 1][i];
this.matrix[size - j - 1][i] = this.matrix[size - i - 1][size - j - 1];
this.matrix[size - i - 1][size - j - 1] = this.matrix[j][size - i - 1];
this.matrix[j][size - i - 1] = temp;
}
}
}
/**
* Trick is after initializing temp,
* Interchange first statement assignee with last's assigned
* Modify second and third assignment alone accordingly
*/
public void rotateminus90() {
for(int i = 0; i < size / 2; i++) {
for(int j = i; j < size - i - 1; j++) {
int temp = this.matrix[i][j];
this.matrix[i][j] = this.matrix[j][size - i - 1];
this.matrix[j][size - i - 1] = this.matrix[size - i - 1][size - j - 1];
this.matrix[size - i - 1][size - j - 1] = this.matrix[size - j - 1][i];
this.matrix[size - j - 1][i] = temp;
}
}
}
public void printAll() {
for(int i = 0; i < size; i++) {
for(int j = 0; j < size; j++) {
System.out.print(String.format("%02d ", matrix[i][j]));
}
System.out.print("\n");
}
System.out.println("***************");
}
}
public static void demoMatrix() {
SquareMatrix matrix = new SquareMatrix(5);
matrix.row(1).column(1).add(1);
matrix.column(2).add(2);
matrix.column(3).add(3);
matrix.column(4).add(4);
matrix.column(5).add(5);
matrix.row(2).column(1).add(6);
matrix.column(2).add(7);
matrix.column(3).add(8);
matrix.column(4).add(9);
matrix.column(5).add(10);
matrix.row(3).column(1).add(11);
matrix.column(2).add(12);
matrix.column(3).add(13);
matrix.column(4).add(14);
matrix.column(5).add(15);
matrix.row(4).column(1).add(16);
matrix.column(2).add(17);
matrix.column(3).add(18);
matrix.column(4).add(19);
matrix.column(5).add(20);
matrix.row(5).column(1).add(21);
matrix.column(2).add(22);
matrix.column(3).add(23);
matrix.column(4).add(24);
matrix.column(5).add(25);
matrix.printAll();
matrix.rotatePlus90_easy();
matrix.printAll();
matrix.rotateMinus90_easy();
matrix.printAll();
matrix.rotate90();
matrix.printAll();
matrix.rotateminus90();
matrix.printAll();
}