Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * This class represnts a Matrix object.
- * @author (Eyal Oren)
- * @version (2020a, 21.11.2019)
- */
- public class Matrix
- {
- private int[][] _matrix;
- private final int DEAFULT_VALUE = 0;
- private int [][] copyArray(int [][] array)
- {
- int [][] newArray = new int[array.length][array[0].length];
- for (int row = 0; row < array.length; row++)
- {
- for (int col = 0; col < array[0].length; col ++)
- {
- newArray[row][col] = array[row][col];
- }
- }
- return newArray;
- }
- public Matrix(int[][] array)
- {
- _matrix = copyArray(array);
- }
- public Matrix(int size1, int size2)
- {
- _matrix = new int[size1][size2];
- for (int row = 0; row < size1; row++)
- {
- for (int col = 0; col < size2; col++)
- {
- _matrix[row][col] = DEAFULT_VALUE;
- }
- }
- }
- public String toString()
- {
- String matrixString = "";
- int numOfRows = _matrix.length;
- int numOfCols = _matrix[0].length;
- for (int row = 0; row < numOfRows ; row++)
- {
- for(int col = 0; col < numOfCols ; col++)
- {
- matrixString += _matrix[row][col];
- if (col < numOfCols - 1)
- matrixString += "\t";
- }
- matrixString += "\n";
- }
- return matrixString;
- }
- public Matrix makeNegative()
- {
- int [][] newArray = new int[_matrix.length][_matrix[0].length];
- int numOfRows = _matrix.length;
- int numOfCols = _matrix[0].length;
- for (int row = 0; row < numOfRows ; row++)
- {
- for(int col = 0; col < numOfCols ; col++)
- {
- newArray[row][col] = 255 - _matrix[row][col];
- }
- }
- return new Matrix(newArray);
- }
- public Matrix imageFilterAverage()
- {
- int [][] newArray = new int[_matrix.length][_matrix[0].length];
- for (int row = 0; row < _matrix.length ; row++)
- for (int col = 0; col < _matrix[0].length; col++)
- {
- int sum = 0;
- int counter = 0;
- int rowLower, rowUpper, colLower, colUpper;
- if (row == 0)
- {
- rowLower = 0;
- rowUpper = row + 1;
- }
- else if (row == _matrix.length - 1)
- {
- rowLower = row - 1;
- rowUpper = row;
- }
- else
- {
- rowLower = row - 1;
- rowUpper = row + 1;
- }
- if (col == 0)
- {
- colLower = 0;
- colUpper = col + 1;
- }
- else if (col == _matrix[0].length - 1)
- {
- colLower = col - 1;
- colUpper = col;
- }
- else
- {
- colLower = col - 1;
- colUpper = col + 1;
- }
- for (int row2 = rowLower; row2 <= rowUpper; row2++)
- for(int col2 = colLower; col2 <= colUpper; col2++)
- {
- sum += _matrix[row2][col2];
- counter ++;
- }
- newArray[row][col] = sum / counter;
- }
- return new Matrix(newArray);
- }
- public Matrix rotateClockwise()
- {
- int [][] newArray = new int[_matrix[0].length][_matrix.length]; //To check if to a return a new matrix
- for (int row = 0; row < _matrix[0].length; row++)
- for (int col = 0; col < _matrix.length; col++)
- {
- newArray[row][col] = _matrix[_matrix.length - col - 1][row];
- }
- return new Matrix(newArray);
- }
- public Matrix rotateCounterClockwise()
- {
- int [][] newArray = new int[_matrix[0].length][_matrix.length];
- for (int row = 0; row < _matrix[0].length; row++)
- for (int col = 0; col < _matrix.length; col++)
- {
- newArray[row][col] = _matrix[col][_matrix[0].length - row - 1];
- }
- return new Matrix(newArray);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement