-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMatrix.java
More file actions
183 lines (174 loc) · 6.73 KB
/
Copy pathMatrix.java
File metadata and controls
183 lines (174 loc) · 6.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/**
* This class represtents a photo matrix.
*
* @author studentp
* @version 15.04.19
*/
public class Matrix {
private int[][] _matrixArray;
/**
* Constructs a Matric from a two-dimensional array, the dimensions as well as
* the values of thes Matrix will be the same as the dimensions and values of
* the two-dimensional array,
*
* @param array array to build upon a new array.
*/
public Matrix(int[][] array) {
int numberOfRows = array.length;
int numberOfColumns = array[0].length;
_matrixArray = new int[numberOfRows][numberOfColumns];
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array[i].length; j++) {
_matrixArray[i][j] = array[i][j];
}
}
}
/**
* Constructs a size1 by size2 Matrix of zeones.
*
* @param size1 the number of the array rows
* @param size2 the number of the array columns
*/
public Matrix(int size1, int size2) {
_matrixArray = new int[size1][size2];
}
/**
* Returns a string representation of this Matrix object.
* Every line represented in a new line
*
* @override toString in class java.lang.Object
*/
public String toString() {
String returnString = new String();
for (int i = 0; i < _matrixArray.length; i++) {
for (int j = 0; j < _matrixArray[i].length; j++) {
returnString += _matrixArray[i][j];
if (j == _matrixArray[i].length - 1) {
returnString += "\n";
} // in case there are no more elements in the line
else {
returnString += "\t";
} // in case there are more elemnts in the line
}
}
return returnString;
}
/**
* Create a negative matrix.
*
* @retern new Matrix with the negative values in each cell.
*/
public Matrix makeNegative() {
Matrix negativeArray = new Matrix(this._matrixArray);
for (int i = 0; i < negativeArray._matrixArray.length; i++) {
for (int j = 0; j < negativeArray._matrixArray[i].length; j++) {
negativeArray._matrixArray[i][j] -= 255;// minus 225 creates the negative value // SHAY: Use finals (-3)
negativeArray._matrixArray[i][j] *= -1;// the value should be positive
}
}
return negativeArray;
}
/**
* Check if a specific loactaion is included in an array.
*
* @param other the array to check if it contains the location
* @return true if the location is inside the bounderies of the array.
*/
private boolean cellExists(Matrix other, int line, int row) {
if (0 <= line && line < other._matrixArray.length &&
0 <= row && row < other._matrixArray[0].length) {
// if the cell index aren't negative
// and if ther'e not bigger than the array length (max row and column index)
return true;
}
return false;
}
/**
* Create a new, softner (more even) matrix.
*
* @retrurn a matrix with more even values.
*/
public Matrix imageFilterAverage() {
int sum = 0, divide = 0;
Matrix copyMatrix = new Matrix(this._matrixArray);
// create a new object to check avarages off cells before chcnges
for (int i = 0; i < copyMatrix._matrixArray.length; i++) {
for (int j = 0; j < copyMatrix._matrixArray[i].length; j++) {
for (int k = i - 1; k <= i + 1; k++) {
for (int p = j - 1; p <= j + 1; p++) {
if (cellExists(copyMatrix, k, p) == true) {
sum += copyMatrix._matrixArray[k][p];
divide++;
} /*
* the method checks elements: in the colume indexes
* before, on,and after the cell (i-1,i,i+1)
* in the row indexes under,on, and above the cell (j-1,j,j+1)
* if there are cells, it adds their values to the sum
* and +1 to the divide
*/
}
}
this._matrixArray[i][j] = sum / divide;
// than it calcultes the avarage and changes the cell value.
sum = 0;
divide = 0; // reset the sum and divide for the next cell.
}
}
return this;
}
/**
* Rotate the matrix 90 degrees right.
*
* @return a right-rotated matrix.
*/
public Matrix rotateClockwise() {
Matrix rightRotated = new Matrix(this._matrixArray[0].length,
this._matrixArray.length);
// the new matrix column number is the the original row number
// and its row number is ther original column number
int k = 0, z = rightRotated._matrixArray[0].length - 1;
for (int i = 0; i < this._matrixArray.length; i++) {
for (int j = 0; j < this._matrixArray[0].length; j++, k++) {
rightRotated._matrixArray[k][z] = this._matrixArray[i][j];
} /*
* the method places one element in each row every time:
* from the farest cell in each column until the lowest one (0),
* while moving up in the rows.
*/
z--;
k = 0;
}
return rightRotated;
}
/**
* Rotate the matrix 90 degrees left.
*
* @return a left-rotated matrix.
*/
public Matrix rotateCounterClockwise() {
Matrix leftRotated = new Matrix(this._matrixArray[0].length,
this._matrixArray.length);
int k = leftRotated._matrixArray.length - 1, z = 0;
for (int i = 0; i < this._matrixArray.length; i++) {
for (int j = 0; j < this._matrixArray[0].length; j++, k--) {
leftRotated._matrixArray[k][z] = this._matrixArray[i][j];
} /*
* the method places one element in each row every time:
* from the closest cell in each column (0) until the farest one.
* while moving down in the rows.
*/
z++;
k = leftRotated._matrixArray.length - 1;
}
return leftRotated;
}
public static void main(String[] args) {
int[][] array = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };
Matrix matrix = new Matrix(array);
System.out.println(matrix);
System.out.println(matrix.makeNegative());
System.out.println(matrix.imageFilterAverage());
System.out.println(matrix.rotateClockwise());
System.out.println(matrix.rotateCounterClockwise());
}
}