woonie

Transformation.java

Jan 30th, 2012
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.68 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. class Matrix {
  4.  private static int[][] rotate(String degree, int[][] anArray) {
  5.     int[][] anotherArray;
  6.     int i, j, k, l, n, end;
  7.     n = anArray.length;
  8.     end = n - 1;
  9.     anotherArray = new int[n][n];
  10.     if (degree.equals("90")){
  11.             for (i = 0; i < n; i++){
  12.                 for (j = 0; j < n; j++){
  13.                     anotherArray[i][j] = anArray[end - j][i];
  14.                 }
  15.             }
  16.     } else if (degree.equals("180")){
  17.             for (i = 0; i < n; i++){
  18.                 for (j = 0; j < n; j++){
  19.                     anotherArray[i][j] = anArray[end - i][end - j];
  20.                 }
  21.             }
  22.     } else if (degree.equals("270")){
  23.             for (i = 0; i < n; i++){
  24.                 for (j = 0; j < n; j++){
  25.                     anotherArray[i][j] = anArray[j][end - i];
  26.                 }
  27.             }
  28.      }
  29.      return anotherArray;
  30.  }     
  31.  private static int[][] reflectX(int[][] anArray) {
  32.     int i, j, k, l, n, end;
  33.     int[][] anotherArray;
  34.     n = anArray.length;
  35.     anotherArray = new int[n][n];
  36.     end = n - 1;
  37.     for (i = 0; i < n; i++){
  38.         for (j = 0; j <n; j++){
  39.             anotherArray[i][j] = anArray[end - i][j];
  40.         }
  41.     }
  42.     return anotherArray;
  43.  } 
  44.  private static int[][] reflectY(int[][] anArray) {
  45.      int i, j, k, l, n, end;
  46.      int[][] anotherArray;
  47.      n = anArray.length;
  48.      anotherArray = new int[n][n];
  49.      end = n - 1;
  50.      for (i = 0; i < n; i++){
  51.          for (j = 0; j < n; j++){
  52.              anotherArray[i][j] = anArray[i][end - j];
  53.          }
  54.      }
  55.      return anotherArray;
  56.  }
  57.  public static int[][] operate(String operation, String type, int[][] anArray) {
  58.      int[][] anotherArray;
  59.      anotherArray = new int[anArray.length][anArray.length];
  60.      if (operation.equals("Rotate")){
  61.          anotherArray = rotate(type, anArray);
  62.      } else if (operation.equals("Reflect")){
  63.          if (type.equals("x")){
  64.              anotherArray = reflectX(anArray);
  65.          } else if (type.equals("y")){
  66.              anotherArray =  reflectY(anArray);
  67.          }
  68.      }
  69.      return anotherArray;
  70.  }
  71. }
  72.  
  73. class Transformation {
  74.   public static void main(String[] args) {
  75.     Scanner myScanner = new Scanner(System.in);
  76.     int firstCheck, secondCheck;
  77.     firstCheck = myScanner.nextInt();
  78.     int[][] anArray;
  79.     anArray = new int[firstCheck][firstCheck];
  80.     for (int j = 0; j < firstCheck; j++){
  81.         for (int k = 0; k < firstCheck; k++){
  82.             int a;
  83.             a = myScanner.nextInt();
  84.             anArray[j][k] = a;
  85.         }
  86.     }
  87.     secondCheck = myScanner.nextInt();
  88.     String thirdCheck, fourthCheck;
  89.     for (int i = 0; i < secondCheck; i++){
  90.         thirdCheck = myScanner.next();
  91.         fourthCheck = myScanner.next();
  92.         anArray = Matrix.operate(thirdCheck, fourthCheck, anArray);
  93.     }
  94.     for (int a = 0; a < firstCheck; a++){
  95.         for (int b = 0; b < firstCheck; b++){
  96.             System.out.print(anArray[a][b]);
  97.             if (b < firstCheck - 1){
  98.             System.out.print(" ");
  99.             }
  100.         }
  101.         if (a < firstCheck - 1){
  102.             System.out.println("");
  103.         }
  104.     }
  105.   }
  106. }
Advertisement
Add Comment
Please, Sign In to add comment