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