line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
2
|
|
|
2
|
|
747
|
use strict; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
61
|
|
2
|
2
|
|
|
2
|
|
9
|
use warnings; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
1335
|
|
3
|
|
|
|
|
|
|
package Math::HexGrid::Hex; |
4
|
|
|
|
|
|
|
$Math::HexGrid::Hex::VERSION = '0.01'; |
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
sub new |
7
|
|
|
|
|
|
|
{ |
8
|
33
|
|
|
33
|
1
|
468
|
my ($class, $q, $r, $s) = @_; |
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
# if s wasnt provided, calculate it |
11
|
33
|
|
100
|
|
|
123
|
$s ||= - $q - $r; |
12
|
|
|
|
|
|
|
|
13
|
33
|
50
|
33
|
|
|
314
|
die 'Invalid coordinates!' |
|
|
|
33
|
|
|
|
|
|
|
|
33
|
|
|
|
|
14
|
|
|
|
|
|
|
unless defined $q && defined $r && defined $s |
15
|
|
|
|
|
|
|
&& $q + $r + $s == 0; |
16
|
|
|
|
|
|
|
|
17
|
33
|
|
|
|
|
271
|
bless { q => $q, r => $r, s => $s }, $class; |
18
|
|
|
|
|
|
|
} |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
sub hex_equal |
22
|
|
|
|
|
|
|
{ |
23
|
1
|
|
|
1
|
1
|
2
|
my ($self, $hex) = @_; |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
$self->{q} == $hex->{q} |
26
|
|
|
|
|
|
|
&& $self->{r} == $hex->{r} |
27
|
1
|
50
|
33
|
|
|
13
|
&& $self->{s} == $hex->{s}; |
28
|
|
|
|
|
|
|
} |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
sub hex_add |
32
|
|
|
|
|
|
|
{ |
33
|
0
|
|
|
0
|
1
|
|
my ($self, $hex) = @_; |
34
|
|
|
|
|
|
|
Math::HexGrid::Hex->new( |
35
|
|
|
|
|
|
|
$self->{q} + $hex->{q}, |
36
|
|
|
|
|
|
|
$self->{r} + $hex->{r}, |
37
|
|
|
|
|
|
|
$self->{s} + $hex->{s}, |
38
|
0
|
|
|
|
|
|
); |
39
|
|
|
|
|
|
|
} |
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
sub hex_subtract |
43
|
|
|
|
|
|
|
{ |
44
|
0
|
|
|
0
|
1
|
|
my ($self, $hex) = @_; |
45
|
|
|
|
|
|
|
Math::HexGrid::Hex->new( |
46
|
|
|
|
|
|
|
$self->{q} - $hex->{q}, |
47
|
|
|
|
|
|
|
$self->{r} - $hex->{r}, |
48
|
|
|
|
|
|
|
$self->{s} - $hex->{s}, |
49
|
0
|
|
|
|
|
|
); |
50
|
|
|
|
|
|
|
} |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
sub hex_multiply |
54
|
|
|
|
|
|
|
{ |
55
|
0
|
|
|
0
|
1
|
|
my ($self, $hex) = @_; |
56
|
|
|
|
|
|
|
Math::HexGrid::Hex->new( |
57
|
|
|
|
|
|
|
$self->{q} * $hex->{q}, |
58
|
|
|
|
|
|
|
$self->{r} * $hex->{r}, |
59
|
|
|
|
|
|
|
$self->{s} * $hex->{s}, |
60
|
0
|
|
|
|
|
|
); |
61
|
|
|
|
|
|
|
} |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
sub hex_length |
64
|
|
|
|
|
|
|
{ |
65
|
0
|
|
|
0
|
0
|
|
my ($self) = @_; |
66
|
0
|
|
|
|
|
|
int((abs($self->{q}) + abs($self->{r}) + abs($self->{s})) / 2); |
67
|
|
|
|
|
|
|
} |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
sub hex_distance |
71
|
|
|
|
|
|
|
{ |
72
|
0
|
|
|
0
|
1
|
|
my ($self, $hex) = @_; |
73
|
0
|
|
|
|
|
|
$self->hex_subtract($hex)->hex_length; |
74
|
|
|
|
|
|
|
} |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
my @hex_directions = ( |
77
|
|
|
|
|
|
|
Math::HexGrid::Hex->new(1,0,-1), |
78
|
|
|
|
|
|
|
Math::HexGrid::Hex->new(1,-1,0), |
79
|
|
|
|
|
|
|
Math::HexGrid::Hex->new(0,-1,1), |
80
|
|
|
|
|
|
|
Math::HexGrid::Hex->new(-1,0,1), |
81
|
|
|
|
|
|
|
Math::HexGrid::Hex->new(-1,1,0), |
82
|
|
|
|
|
|
|
Math::HexGrid::Hex->new(0,1,-1), |
83
|
|
|
|
|
|
|
); |
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
sub _hex_direction |
86
|
|
|
|
|
|
|
{ |
87
|
|
|
|
|
|
|
# this will handle directions > 6 and < 0 |
88
|
0
|
|
|
0
|
|
|
$hex_directions[(6 + ($_[0] % 6)) % 6]; |
89
|
|
|
|
|
|
|
} |
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
sub hex_neighbor |
93
|
|
|
|
|
|
|
{ |
94
|
0
|
|
|
0
|
1
|
|
my ($self, $direction) = @_; |
95
|
0
|
|
|
|
|
|
$self->hex_add(_hex_direction($direction)); |
96
|
|
|
|
|
|
|
} |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
1; |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
__END__ |