-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmatrix.pl
59 lines (54 loc) · 1.33 KB
/
matrix.pl
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
package Matrix;
# constructor expects an x-dimension and a y-dimension
sub new {
my $class = shift;
my $self = {
_xDim => shift,
_yDim => shift
};
bless $self, $class;
return $self;
}
# this method expects an x-coordinate and a y-coordinate
sub getValue {
$self = shift;
$x = shift;
$y = shift;
if (defined($self->{ _matrixArray })) {
return $self->{ _matrixArray }[$self->getMatrixIndex($x, $y)];
} else {
return 0;
}
}
# this method expects an x-coordinate, a y-coordinate, and a value
sub setValue {
$self = shift;
$x = shift;
$y = shift;
$value = shift;
$self->{ _matrixArray }[$self->getMatrixIndex($x, $y)] = $value;
}
# this method prints out the matrix in an easy to read format. it allows for a parameter which tells what method to run during the print. mostly for debugging purposes.
sub print {
$self = shift;
$method = shift;
for ($r = 1; $r <= $self->{ _yDim }; $r++) {
for ($c = 1; $c <= $self->{ _xDim }; $c++) {
$val = $self->{ _matrixArray }[$self->getMatrixIndex($c, $r)];
if (!defined $val || $method eq "") {
$printVal = $val;
} else {
$printVal = $val->$method;
}
print " $printVal ";
}
print "\n";
}
}
sub getMatrixIndex {
$self = shift;
$x = shift;
$y = shift;
return $x - 1 + $self->{ _xDim } * ($y - 1);
}
1;