blib/lib/Math/HexGrid.pm untested

filter: Statementstmt 0.0% 0 / 50 Branchbran 0.0% 0 / 4 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 9 Podpod 0.0% 0 / 5 totaltotal 0.0% 0 / 68 CC 3 SCAR 15.5
CC
3
Cov
0%
count_sides
12.0
new_hexagon
2.0
new_triangle
2.0
CRAP 4.7
1 use strict;
2 use warnings;
3 package Math::HexGrid;
4 $Math::HexGrid::VERSION = '0.02';
5 # ABSTRACT: Math::HexGrid - create hex coordinate grids
6  
7 use Math::HexGrid::Hex;
8 use List::Util qw/min max/;
9  
10  
11  
12  
13 sub new_hexagon
14 {
15 my ($class, $radius) = @_;
16  
17 my %map;
18 for (my $q = - $radius; $q <= $radius; $q++)
19 {
20 my $r1 = max(-$radius, -$q - $radius);
21 my $r2 = min($radius, -$q + $radius);
22  
23 for (my $r = $r1; $r <= $r2; $r++)
24 {
25 $map{"$q$r"} = Math::HexGrid::Hex->new($q, $r);
26 }
27 }
28  
29 bless {
30 map => \%map,
31 type => 'hexagon',
32 }, $class;
33 }
34  
35  
36 sub new_triangle
37 {
38 my ($class, $rows) = @_;
39  
40 my %map;
41 for (my $q = 0; $q <= $rows; $q++)
42 {
43 for (my $r = 0; $r <= $rows - $q; $r++)
44 {
45 $map{"$q$r"} = Math::HexGrid::Hex->new($q, $r);
46 }
47 }
48 bless {
49 map => \%map,
50 type => 'triangle',
51 }, $class;
52 }
53  
54  
55 sub hexgrid { $_[0]->{map} }
56  
57  
58 sub hex
59 {
60 my ($self, $q, $r) = @_;
61 $self->{map}{"$q$r"};
62 }
63  
64  
65 sub count_sides
66 {
67 my ($self) = @_;
68 my $n = keys %{$self->{map}};
69  
70 if ($self->{type} eq 'hexagon')
71 {
72 ($n-1) * 3 + $n-1 + 6;
73 }
74 elsif ($self->{type} eq 'triangle')
75 {
76 $n * 4 + 3;
77 }
78 else
79 {
80 die "Unknown map type!";
81 }
82 }
83  
84  
85 1;
86  
87 __END__