Advertisement
Guest User

Untitled

a guest
Dec 23rd, 2019
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.55 KB | None | 0 0
  1. /**
  2.  * This class represnts a Matrix object.
  3.  * @author (Eyal Oren)
  4.  * @version (2020a, 21.11.2019)
  5.  */
  6.  
  7. public class Matrix
  8. {
  9.     private int[][] _matrix;
  10.     private final int DEAFULT_VALUE = 0;
  11.    
  12.     private int [][] copyArray(int [][] array)
  13.     {
  14.         int [][] newArray = new int[array.length][array[0].length];
  15.         for (int row = 0; row < array.length; row++)
  16.         {    
  17.             for (int col = 0; col < array[0].length; col ++)
  18.             {
  19.                 newArray[row][col] = array[row][col];
  20.             }
  21.         }    
  22.         return newArray;
  23.     }
  24.    
  25.     public Matrix(int[][] array)
  26.     {
  27.         _matrix = copyArray(array);
  28.     }
  29.    
  30.     public Matrix(int size1, int size2)
  31.     {
  32.         _matrix = new int[size1][size2];
  33.         for (int row = 0; row < size1; row++)
  34.         {
  35.             for (int col = 0; col < size2; col++)
  36.             {
  37.                 _matrix[row][col] = DEAFULT_VALUE;
  38.             }
  39.         }  
  40.     }
  41.    
  42.     public String toString()
  43.     {  
  44.         String matrixString = "";
  45.         int numOfRows = _matrix.length;
  46.         int numOfCols = _matrix[0].length;
  47.         for (int row = 0; row < numOfRows ; row++)
  48.         {
  49.             for(int col = 0; col < numOfCols ; col++)
  50.             {
  51.                 matrixString += _matrix[row][col];
  52.                 if (col < numOfCols - 1)
  53.                     matrixString += "\t";
  54.             }
  55.             matrixString += "\n";
  56.         }
  57.         return matrixString;
  58.     }
  59.     public Matrix makeNegative()
  60.     {
  61.         int [][] newArray = new int[_matrix.length][_matrix[0].length];
  62.         int numOfRows = _matrix.length;
  63.         int numOfCols = _matrix[0].length;
  64.        
  65.         for (int row = 0; row < numOfRows ; row++)
  66.         {
  67.             for(int col = 0; col < numOfCols ; col++)
  68.             {
  69.                 newArray[row][col] = 255 - _matrix[row][col];
  70.             }
  71.         }
  72.         return new Matrix(newArray);
  73.     }
  74.    
  75.    
  76.     public Matrix imageFilterAverage()
  77.     {
  78.         int [][] newArray = new int[_matrix.length][_matrix[0].length];
  79.         for (int row = 0; row < _matrix.length ; row++)
  80.             for (int col = 0; col < _matrix[0].length; col++)
  81.             {
  82.                 int sum = 0;
  83.                 int counter = 0;
  84.                
  85.                 int rowLower, rowUpper, colLower, colUpper;
  86.                
  87.                 if (row == 0)
  88.                 {
  89.                      rowLower = 0;
  90.                      rowUpper = row + 1;
  91.                 }
  92.                 else if (row == _matrix.length - 1)
  93.                 {
  94.                     rowLower = row - 1;
  95.                     rowUpper = row;
  96.                 }
  97.                 else
  98.                 {
  99.                     rowLower = row - 1;
  100.                     rowUpper = row + 1;
  101.                 }
  102.                
  103.                 if (col == 0)
  104.                 {
  105.                      colLower = 0;
  106.                      colUpper = col + 1;
  107.                 }
  108.                 else if (col == _matrix[0].length - 1)
  109.                 {
  110.                     colLower = col - 1;
  111.                     colUpper = col;
  112.                 }
  113.                 else
  114.                 {
  115.                     colLower = col - 1;
  116.                     colUpper = col + 1;
  117.                 }    
  118.                 for (int row2 = rowLower; row2 <= rowUpper; row2++)
  119.                     for(int col2 = colLower; col2 <= colUpper; col2++)
  120.                     {
  121.                         sum += _matrix[row2][col2];
  122.                         counter ++;  
  123.                     }
  124.                 newArray[row][col] = sum / counter;
  125.             }
  126.         return new Matrix(newArray);    
  127.     }
  128.    
  129.     public Matrix rotateClockwise()
  130.     {
  131.         int [][] newArray = new int[_matrix[0].length][_matrix.length]; //To check if to a return a new matrix
  132.         for (int row = 0; row < _matrix[0].length; row++)
  133.             for (int col = 0; col < _matrix.length; col++)
  134.             {
  135.                 newArray[row][col] = _matrix[_matrix.length - col - 1][row];
  136.             }
  137.         return new Matrix(newArray);    
  138.     }
  139.     public Matrix rotateCounterClockwise()
  140.     {
  141.        int [][] newArray = new int[_matrix[0].length][_matrix.length];
  142.        for (int row = 0; row < _matrix[0].length; row++)
  143.            for (int col = 0; col < _matrix.length; col++)
  144.            {
  145.                newArray[row][col] = _matrix[col][_matrix[0].length - row - 1];
  146.            }
  147.        return new Matrix(newArray);    
  148.     }
  149.    
  150. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement