File Coverage

blib/lib/Imager/Draw/Hexagon.pm
Criterion Covered Total %
statement 29 29 100.0
branch 2 4 50.0
condition 2 6 33.3
subroutine 9 9 100.0
pod 6 6 100.0
total 48 54 88.8


line stmt bran cond sub pod time code
1             package Imager::Draw::Hexagon;
2             $Imager::Draw::Hexagon::VERSION = '0.0102';
3 1     1   659 use strict;
  1         2  
  1         37  
4 1     1   576 use Moo;
  1         12258  
  1         5  
5 1     1   1295 use Imager;
  1         3  
  1         8  
6              
7              
8             =head1 NAME
9              
10             Imager::Draw::Hexagon - Draw hexes easily using Imager
11              
12             =head1 VERSION
13              
14             version 0.0102
15              
16             =head1 SYNOPSIS
17              
18             use Imager::Draw::Hexagon;
19              
20             my $hex = Imager::Draw::Hexagon->new( image => $image, side_length => 100 );
21             $hex->draw(color => 'blue');
22              
23             =head1 DESCRIPTION
24              
25             Drawing hexagons requires calculating all the points in the hex. It's harder than it sounds. I figured since I was solving it, I might as well solve it for everyone, so this module was born.
26              
27             =head1 METHODS
28              
29             =head2 new(image => $image, side_length => 100)
30              
31             Constructor.
32              
33             =over
34              
35             =item image
36              
37             The L object to draw the hex on to. Required.
38              
39             =item side_length
40              
41             The length of each side of the hexagon in pixels. Required.
42              
43             =item x
44              
45             The x coordinate of the top left corner to start drawing the hex. Defaults to 0.
46              
47             =item y
48              
49             The y coordinate of the top left corner to start drawing the hex. Defaults to 0.
50              
51             =back
52              
53             =cut
54              
55             =head2 x()
56              
57             Get or set the x coordinate of the top left corner of where to start drawing the hex.
58              
59             =cut
60              
61             has x => (
62             is => 'rw',
63             default => sub { 0 },
64             );
65              
66             =head2 y()
67              
68             Get or set the y coordinate of the top left corner of where to start drawing the hex.
69              
70             =cut
71              
72             has y => (
73             is => 'rw',
74             default => sub { 0 },
75             );
76              
77             =head2 image()
78              
79             Get or set the L object.
80              
81             =cut
82              
83             has image => (
84             is => 'rw',
85             required => 1,
86             );
87              
88             =head2 side_length()
89              
90             Get or set the length, in pixels, of each side of the hex.
91              
92             =cut
93              
94             has side_length => (
95             is => 'rw',
96             required => 1,
97             );
98              
99             =head2 short_leg()
100              
101             Hexes are essentially a square with a series of right trangles drawn around them. This is the short leg of that triangle or half the value of the side length.
102              
103             =cut
104              
105             sub short_leg {
106 16     16 1 18 my $self = shift;
107 16         98 return $self->side_length * 0.5;
108             }
109              
110             =head2 long_leg()
111              
112             Hexes are essentially a square with a series of right trangles drawn around them. This is the long leg of that triangle or the side length multiplied by 0.866 (sin(60)).
113              
114             =cut
115              
116             sub long_leg {
117 16     16 1 16 my $self = shift;
118 16         97 return $self->side_length * 0.866; # sin(60)
119             }
120              
121             =head2 ew_coords()
122              
123             Returns an array ref of coordinent pairs if the hex is to be drawn with a flat top (east-west).
124              
125             =cut
126              
127             sub ew_coords {
128 2     2 1 2199 my $self = shift;
129             return [
130 2         23 [ int($self->x), int($self->y + $self->long_leg) ],
131             [ int($self->x + $self->short_leg), int($self->y) ],
132             [ int($self->x + $self->short_leg + $self->side_length), int($self->y) ],
133             [ int($self->x + (2 * $self->side_length)), int($self->y + $self->long_leg) ],
134             [ int($self->x + $self->short_leg + $self->side_length), int($self->y + (2 * $self->long_leg)) ],
135             [ int($self->x + $self->short_leg), int($self->y + (2 * $self->long_leg)) ],
136             ];
137             }
138              
139             =head2 ns_coords()
140              
141             Returns an array ref of coordinent pairs if the hex is to be drawn with a peaked top (north-south).
142              
143             =cut
144              
145             sub ns_coords {
146 2     2 1 4 my $self = shift;
147             return [
148 2         13 [ int($self->x), int($self->y + $self->short_leg) ],
149             [ int($self->x + $self->long_leg), int($self->y) ],
150             [ int($self->x + (2 * $self->long_leg)), int($self->y + $self->short_leg) ],
151             [ int($self->x + (2 * $self->long_leg)), int($self->y + $self->short_leg + $self->side_length) ],
152             [ int($self->x + $self->long_leg), int($self->y + (2 * $self->side_length)) ],
153             [ int($self->x), int($self->y + $self->short_leg + $self->side_length) ],
154             ];
155             }
156              
157             =head2 outline()
158              
159             Call this to draw an outline of a hex on the image. It accepts all the same parameters as L, plus:
160              
161             =over
162              
163             =item direction
164              
165             Defaults to C. Options are C and C.
166              
167             =back
168              
169             =cut
170              
171             sub outline {
172 1     1 1 4 my $self = shift;
173 1         4 my %params = @_;
174 1 50 33     10 $params{points} = (exists $params{direction} && $params{direction} eq 'ns') ? $self->ns_coords : $self->ew_coords;
175 1         3 delete $params{direction};
176 1         1 push @{$params{points}}, $params{points}[0];
  1         5  
177 1         11 $self->image->polyline(%params);
178             }
179              
180             =head2 draw()
181              
182             Call this to draw a filled hex on the image. It accepts all the same parameters as L, plus:
183              
184             =over
185              
186             =item direction
187              
188             Defaults to C. Options are C and C.
189              
190             =back
191              
192             =cut
193              
194             sub draw {
195 1     1 1 2 my $self = shift;
196 1         4 my %params = @_;
197 1 50 33     11 $params{points} = (exists $params{direction} && $params{direction} eq 'ns') ? $self->ns_coords : $self->ew_coords;
198 1         3 delete $params{direction};
199 1         10 $self->image->polygon(%params);
200             }
201              
202             =head1 TODO
203              
204             None that I can think of at this time.
205              
206             =head1 PREREQS
207              
208             L
209             L
210              
211             =head1 SUPPORT
212              
213             =over
214              
215             =item Repository
216              
217             L
218              
219             =item Bug Reports
220              
221             L
222              
223             =back
224              
225              
226             =head1 AUTHOR
227              
228             =over
229              
230             =item JT Smith
231              
232             =back
233              
234             =head1 LEGAL
235              
236             Imager::Draw::Hexagon is Copyright 2014 Plain Black Corporation (L) and is licensed under the same terms as Perl itself.
237              
238             =cut
239              
240             1;