-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMatrix.java
More file actions
146 lines (135 loc) · 3.72 KB
/
Matrix.java
File metadata and controls
146 lines (135 loc) · 3.72 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
/**
* Write a description of class Matrix here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Matrix
{
// instance variables
private int[][] _matrix;
/**
* Constructs a Matrix from a two-dimensional array. The dimensions
* as well as the values of this matrix will be the same as the
* dimensionsand values of the values of the 2D array.
*/
public Matrix(int[][] array)
{
// parameters to shorten text
int len = array.length;
int height = array[0].length;
// initialize size
_matrix = new int[len][height];
// Copy 2D elements
for (int i = 0; i < len ; i++)
{
for (int j = 0; j < height; j++)
{
_matrix[i][j] = array[i][j];
}
}
}
/**
* Constructs a size1 by size2 Matrix of zeroes.
*/
public Matrix(int size1, int size2)
{
_matrix = new int[size1][size2];
for (int i = 0; i < size1 ; i++)
{
for (int j = 0; j < size2; j++)
{
_matrix[i][j] = 0;
}
}
}
/**
*
* @return
*/
public String toString()
{
String theMatrix= "";
for (int i = 0; i < _matrix.length; i++)
{
for (int j = 0; j < _matrix[0].length; j++)
{
// Avoid adding a \t at the end of each line
if (j != _matrix[0].length -1)
theMatrix = theMatrix + _matrix[i][j] + "\t";
else
theMatrix = theMatrix + _matrix[i][j];
}
theMatrix = theMatrix + "\n";
}
return theMatrix;
}
public Matrix makeNegative()
{
Matrix negativeMatrix = new Matrix(_matrix);
for (int i = 0; i < _matrix.length; i++)
{
for (int j = 0; j < _matrix[0].length; j++)
{
negativeMatrix._matrix[i][j] = 255 - negativeMatrix._matrix[i][j];
}
}
return negativeMatrix;
}
// Check if place is in matrix
private boolean inMatrix(int line, int col)
{
if ( _matrix.length > line && line >= 0 && _matrix[0].length > col
&& col >= 0)
return true;
return false;
}
// Get area average
private int areaAverage(int line, int col)
{
int sum = 0;
int counter = 0;
for (int i = line - 1; i <= line + 1; i++)
{
for (int j = col - 1; j <= col + 1; j++)
{
if (inMatrix(i, j))
{
sum += _matrix[i][j];
counter ++;
}
}
}
return (int)(sum / counter);
}
public Matrix imageFilterAverage()
{
Matrix newMatrix = new Matrix(_matrix.length,_matrix[0].length);
for (int i = 0; i < _matrix.length; i++)
{
for (int j = 0; j < _matrix[0].length; j++)
{
newMatrix._matrix[i][j] = areaAverage(i, j);
}
}
return newMatrix;
}
public Matrix rotateClockwise()
{
Matrix newMatrix = new Matrix(_matrix[0].length,_matrix.length);
for (int i = 0; i < _matrix.length; i++)
{
for (int j = 0; j < _matrix[0].length; j++)
{
newMatrix._matrix[j][_matrix.length -1 -i] = _matrix[i][j];
}
}
return newMatrix;
}
public Matrix rotateCounterClockwise()
{
Matrix newMatrix = new Matrix(_matrix);
newMatrix = newMatrix.rotateClockwise().rotateClockwise().rotateClockwise();
return newMatrix;
}
}