blib/lib/Imager/Draw/Hexagon.pm

filter: Statementstmt 94.5% 35 / 37 Branchbran 50.0% 2 / 4 Conditioncond 33.3% 2 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 81.8% 9 / 11 Podpod 100% 6 / 6 totaltotal 79.4% 54 / 68 CC 5 SCAR 7.0
CC
5
Cov
83%
draw
3.2
outline
3.1
__ANON__
2.0
CRAP 2.0
↑ Summary
1 package Imager::Draw::Hexagon;
2 1 $Imager::Draw::Hexagon::VERSION = '0.0101';
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use Imager;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7  
8 =head1 NAME
9  
10 Imager::Draw::Hexagon - Draw hexes easily using Imager
11  
12 =head1 VERSION
13  
14 version 0.0101
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<Imager> 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 0 ▶ default => sub { 0 },
Subroutine __ANON__: not called CC=1 SCAR=6.9
64 1 );
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 0 ▶ default => sub { 0 },
Subroutine __ANON__: not called CC=1 SCAR=6.9
75 1 );
76  
77 =head2 image()
78  
79 Get or set the L<Imager> object.
80  
81 =cut
82  
83 1 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 1 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 ▶ my $self = shift;
Subroutine short_leg: called CC=1 SCAR=0.0
Pod documented
107 16 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 ▶ my $self = shift;
Subroutine long_leg: called CC=1 SCAR=0.0
Pod documented
118 16 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 ▶ my $self = shift;
Subroutine ew_coords: called CC=1 SCAR=0.0
Pod documented
129 return [
130 2 [ 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 ▶ my $self = shift;
Subroutine ns_coords: called CC=1 SCAR=0.0
Pod documented
147 return [
148 2 [ 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<Imager::Draw/polyline>, plus:
160  
161 =over
162  
163 =item direction
164  
165 Defaults to C<ew>. Options are C<ew> and C<ns>.
166  
167 =back
168  
169 =cut
170  
171 sub outline {
172 1 ▶ my $self = shift;
Subroutine outline: called CC=3 SCAR=11.4
Pod documented
173 1 my %params = @_;
174 1 ▶ $params{points} = (exists $params{direction} && $params{direction} eq 'ns') ? $self->ns_coords : $self->ew_coords;
Branch: 1 branch
BranchTrueFalseTotal
exists $params{'direction'} && $params{'direction'} eq 'ns' ? : 0 1 1
Condition33%1/3
exists $params{"direction"} and $params{"direction"} eq "ns"
!ll&&!rl&&r
100
MC/DC0%0/2
exists $params{"direction"} and $params{"direction"} eq "ns"
exists $params{"direction"} $params{"direction"} eq "ns"
Truth table33%1/3
A and B
A= exists $params{"direction"}B= $params{"direction"} eq "ns"
ABresult
0X0
100
111
175 1 delete $params{direction};
176 1 push @{$params{points}}, $params{points}[0];
177 1 $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<Imager::Draw/polygon>, plus:
183  
184 =over
185  
186 =item direction
187  
188 Defaults to C<ew>. Options are C<ew> and C<ns>.
189  
190 =back
191  
192 =cut
193  
194 sub draw {
195 1 ▶ my $self = shift;
Subroutine draw: called CC=3 SCAR=11.8
Pod documented
196 1 my %params = @_;
197 1 ▶ $params{points} = (exists $params{direction} && $params{direction} eq 'ns') ? $self->ns_coords : $self->ew_coords;
Branch: 1 branch
BranchTrueFalseTotal
exists $params{'direction'} && $params{'direction'} eq 'ns' ? : 1 0 1
Condition33%1/3
exists $params{"direction"} and $params{"direction"} eq "ns"
!ll&&!rl&&r
001
MC/DC0%0/2
exists $params{"direction"} and $params{"direction"} eq "ns"
exists $params{"direction"} $params{"direction"} eq "ns"
Truth table33%1/3
A and B
A= exists $params{"direction"}B= $params{"direction"} eq "ns"
ABresult
0X0
100
111
198 1 delete $params{direction};
199 1 $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<Moo>
209 L<Imager>
210  
211 =head1 SUPPORT
212  
213 =over
214  
215 =item Repository
216  
217 L<http://github.com/rizen/Imager-Draw-Hexagon>
218  
219 =item Bug Reports
220  
221 L<http://github.com/rizen/Imager-Draw-Hexagon/issues>
222  
223 =back
224  
225  
226 =head1 AUTHOR
227  
228 =over
229  
230 =item JT Smith <jt_at_plainblack_dot_com>
231  
232 =back
233  
234 =head1 LEGAL
235  
236 Imager::Draw::Hexagon is Copyright 2014 Plain Black Corporation (L<http://www.plainblack.com>) and is licensed under the same terms as Perl itself.
237  
238 =cut
239  
240 1 1;
↑ Summary