package chapter_1;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.Scanner;

/**
 * Question 1.7: 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?
 * 
 * @author Sudharsanan Muralidharan
 */
public class RotateMatrix {

  private int[][] rotate(int[][] matrix, int degrees) {

    int layers = (int) Math.ceil(matrix.length / 2.0);
    System.out.println(layers);

    int top, left, right, bottom;

    for (int i = 0; i < layers; i++) {
      int len = matrix.length - 1;
      top = left = i;
      bottom = right = len - i;
      int temp = 0;

      for (int j = i; j < len - i; j++) {
        temp = matrix[top][j];
        matrix[top][j] = matrix[len - j][left];
        matrix[len - j][left] = matrix[bottom][len - j];
        matrix[bottom][len - j] = matrix[j][right];
        matrix[j][right] = temp;
      }

    }

    printMatrix(matrix);

    return matrix;
  }

  private void printMatrix(int[][] matrix) {
    for (int i = 0; i < matrix.length; i++) {
      for (int j = 0; j < matrix.length; j++) {
        System.out.print(matrix[i][j] + " ");
      }
      System.out.println();
    }
  }

  /**
   * 
   * @param args
   */
  public static void main(String[] args) {
    RotateMatrix rotateMatrix = new RotateMatrix();
    StringBuilder builder = new StringBuilder();
    int[][] matrix;
    int n = 0;
    try {
      FileReader fileReader = new FileReader("input_files/image_matrix");
      Scanner scanner = new Scanner(fileReader);
      while (scanner.hasNextLine()) {
        builder.append(scanner.nextLine().trim()).append("\n");
        n++;
      }

      matrix = new int[n][n];
      String[] lines = builder.toString().split("\n");

      for (int i = 0; i < lines.length; i++) {
        String[] rows = lines[i].split(" ");
        for (int j = 0; j < rows.length; j++) {
          matrix[i][j] = Integer.parseInt(rows[j]);
        }
      }

      System.out.println("Original Image Matrix: ");
      rotateMatrix.printMatrix(matrix);

      rotateMatrix.rotate(matrix, 90);

      scanner.close();
    } catch (FileNotFoundException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
  }
}

@@ -368,16 +368,401 @@
 trix %7B%0A%0A
+  /**%0A   * Rotates the matrix in-place. The number of layers is ceil(n/2) where n is%0A   * the matrix length. For each layer shift the elements of top, left, right%0A   * and bottom without using an extra array of size n.%0A   * %0A   * temp = top%0A   * top = left%0A   * left = bottom%0A   * bottom = right%0A   * right = temp%0A   * %0A   * @param matrix%0A   * @param degrees%0A   * @return matrix%0A   */%0A
   privat
@@ -809,17 +809,16 @@
 rees) %7B%0A
-%0A
     int 
@@ -867,40 +867,8 @@
 .0);
-%0A    System.out.println(layers);
 %0A%0A  
@@ -900,16 +900,135 @@
 ottom;%0A%0A
+    // Rotate (degrees/90) times%0A    for (int k = 0; k %3C (degrees / 90); k++) %7B%0A      // For each layer do the shift%0A  
     for 
@@ -1060,24 +1060,26 @@
 ++) %7B%0A      
+ 
+ 
 int len = ma
@@ -1095,16 +1095,18 @@
 th - 1;%0A
+  
       to
@@ -1125,16 +1125,18 @@
 ;%0A      
+  
 bottom =
@@ -1155,24 +1155,26 @@
  - i;%0A      
+ 
+ 
 int temp = 0
@@ -1176,16 +1176,61 @@
 p = 0;%0A%0A
+        // shift the elements of each side%0A  
       fo
@@ -1255,32 +1255,34 @@
 len - i; j++) %7B%0A
+  
         temp = m
@@ -1300,24 +1300,26 @@
 j%5D;%0A        
+  
 matrix%5Btop%5D%5B
@@ -1342,24 +1342,26 @@
 - j%5D%5Bleft%5D;%0A
+  
         matr
@@ -1401,24 +1401,26 @@
 %5D%5Blen - j%5D;%0A
+  
         matr
@@ -1463,24 +1463,26 @@
 t%5D;%0A        
+  
 matrix%5Bj%5D%5Bri
@@ -1494,24 +1494,34 @@
 = temp;%0A
+        %7D%0A
       %7D%0A
 %0A    %7D%0A%0A
@@ -1508,25 +1508,24 @@
   %7D%0A      %7D%0A
-%0A
     %7D%0A%0A    p
@@ -1527,53 +1527,81 @@
     
-printMatrix(
+return 
 matrix
-)
 ;%0A
-%0A    return
+  %7D%0A%0A  /**%0A   * Prints the matrix%0A   * @param
  matrix
-;
 %0A  
-%7D%0A
+ */
 %0A  p
@@ -2176,16 +2176,53 @@
 eader);%0A
+      %0A      // Read input from file%0A
       wh
@@ -2682,17 +2682,16 @@
  Matrix:
- 
 %22);%0A    
@@ -2737,38 +2737,125 @@
     
-rotateMatrix.rotate(matrix, 90
+System.out.println(%22%5CnAfter Rotation:%22);%0A      rotateMatrix.rotate(matrix, 90);%0A      rotateMatrix.printMatrix(matrix
 );%0A%0A

Question 1.7 done