blib/lib/Math/Grid/Coordinates.pm

filter: Statementstmt 88.5% 62 / 70 Branchbran 50.0% 4 / 8 Conditioncond 45.4% 5 / 11 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 92.3% 12 / 13 Podpod 100% 6 / 6 totaltotal 78.0% 89 / 114 SCAR 29.6
CC
17
Cov
80%
positions
8.6
sequence
6.8
__ANON__
5.4
CRAP 19.4
↑ Summary
1 package Math::Grid::Coordinates;
2  
3 2 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use Moose::Util::TypeConstraints;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 2 ▶use List::AllUtils qw/max/;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use POSIX qw/ceil/;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 # number of items
10 2 has [ qw/grid_width grid_height/ ] => ( is => 'rw', isa => 'Int' );
11  
12 # item size
13 2 has [ qw/item_width item_height/ ] => ( is => 'rw', isa => 'Num' );
14  
15 # gutter border
16 2 has [ qw/gutter border/ ] => ( is => 'rw', isa => 'Num' );
17  
18 1 ▶subtype 'Seq', as 'Str', where { /[h,v]/i };
Subroutine __ANON__: called CC=1 SCAR=0.0
19 0 ▶has arrange => ( is => 'rw', isa => 'Seq', default => sub { 'h' } );
Subroutine __ANON__: not called CC=1 SCAR=1.2
20  
21 # has item_sizes => ( is => 'rw', isa => 'ArrayRef[ArrayRef[Num]]' );
22  
23  
24 around BUILDARGS => sub {
25 1 ▶ my $orig = shift;
Subroutine __ANON__: called CC=5 SCAR=16.8
26 1 my $class = shift;
27  
28 1 ▶ if ( !ref $_[0] ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not ref $_[0]) { } 1 0 1
29
30 1 my %h;
31 1 @h{qw/grid_width grid_height item_width item_height gutter border/} = grep { /[^a-z]/i } @_;
32  
33 1 ▶ ($h{arrange}) = (grep { /^[h,v]i/ } @_) || ('h');
Condition50%1/2
grep({/^[h,v]i/;} @_) || 'h'
l!l
01
MC/DC0%0/1
grep({/^[h,v]i/;} @_) || 'h'
grep({/^[h,v]i/;} @_)
Truth table50%1/2
A
A= grep({/^[h,v]i/;} @_)
Aresult
11
00
34 1 ▶ $h{gutter} ||= 0;
Condition50%1/2
$h{"gutter"} ||= 0
l!l
10
MC/DC0%0/1
$h{"gutter"} ||= 0
$h{"gutter"}
Truth table50%1/2
A
A= $h{"gutter"}
Aresult
11
00
35 1 ▶ $h{border} ||= 0;
Condition50%1/2
$h{"border"} ||= 0
l!l
01
MC/DC0%0/1
$h{"border"} ||= 0
$h{"border"}
Truth table50%1/2
A
A= $h{"border"}
Aresult
11
00
36 1 return $class->$orig( \%h );
37 } else {
38 0 return $class->$orig(@_);
39 }
40 2 };
41  
42 sub total_height {
43 1 ▶ my $self = shift;
Subroutine total_height: called CC=1 SCAR=0.0
Pod documented
44 1 return $self->border + $self->item_height * $self->grid_height + $self->gutter * ($self->grid_height - 1) + $self->border
45 };
46  
47 sub total_width {
48 1 ▶ my $self = shift;
Subroutine total_width: called CC=1 SCAR=0.0
Pod documented
49 1 return $self->border + $self->item_width * $self->grid_width + $self->gutter * ($self->grid_width - 1) + $self->border
50 };
51  
52 sub bbox {
53 1 ▶ my $self = shift;
Subroutine bbox: called CC=2 SCAR=7.1
Pod documented
54 1 my ($w, $h) = ($self->total_width, $self->total_height);
55 1 ▶ return wantarray ? ($w, $h) : [ $w, $h ];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 1 0 1
56 }
57  
58 sub sequence {
59 2 ▶ my $self = shift;
Subroutine sequence: called CC=6 SCAR=19.2
Pod documented
60 2 my ($gw, $gh) = map { $self->$_ } qw/grid_width grid_height/;
61 2 my @sequence;
62  
63 2 ▶ if (lc($self->arrange) eq 'v') {
Branch: 1 branch
BranchTrueFalseTotal
if (lc $self->arrange eq 'v') { } 0 2 2
64 0 for my $x (0..$gw-1) { for my $y (0..$gh-1) { push @sequence, [$x, $y] } }
65 } else {
66 2 for my $y (0..$gh-1) { for my $x (0..$gw-1) { push @sequence, [$x, $y] } }
67 }
68 2 return @sequence;
69 }
70  
71 # sub position {
72 # my $self = shift;
73 # my ($x, $y, $x1, $y1, $x2, $y2) = @_;
74 # my ($gw, $gh, $iw, $ih, $gt, $pb) = map { $self->$_ } qw/grid_width grid_height item_width item_height gutter border/;
75  
76 # # $_->[0] = $iw * $_->[0] + ($gt * $_->[0]) + $pb;
77 # # $_->[1] = $ih * $_->[1] + ($gt * $_->[1]) + $pb
78 # }
79  
80 sub positions {
81 1 ▶ my $self = shift;
Subroutine positions: called CC=7 SCAR=21.5
Pod documented
82 1 my ($x1, $y1, $x2, $y2) = @_;
83 1 ▶ for ($x1, $y1) { $_ ||= 0 };
Condition50%1/2
$_ ||= 0
l!l
02
MC/DC0%0/1
$_ ||= 0
$_
Truth table50%1/2
A
A= $_
Aresult
11
00
84 1 my ($gw, $gh, $iw, $ih, $gt, $pb) = map { $self->$_ } qw/grid_width grid_height item_width item_height gutter border/;
85  
86 # first assign positions in terms of page coordinates
87 1 my @grid = $self->sequence;
88  
89 # then calculate and assign the actual position
90 1 for (@grid) {
91 12 $_->[0] = $iw * $_->[0] + ($gt * $_->[0]) + $pb + $x1;
92 12 $_->[1] = $ih * $_->[1] + ($gt * $_->[1]) + $pb + $y1
93 };
94  
95 1 ▶ if (defined $x2 || defined $y2) { for (@grid) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $x2 or defined $y2) 0 1 1
Condition33%1/3
defined $x2 or defined $y2
l!l&&r!l&&!r
001
MC/DC0%0/2
defined $x2 or defined $y2
defined $x2 defined $y2
Truth table33%1/3
A or B
A= defined $x2B= defined $y2
ABresult
1X1
011
000
96 0 $_->[2] = $_->[0] + $x2;
97 0 $_->[3] = $_->[1] + $y2;
98 } }
99
100 1 return @grid;
101 }
102  
103 sub numbers {
104 1 ▶ my $self = shift;
Subroutine numbers: called CC=1 SCAR=0.0
Pod documented
105 1 return (1..($self->grid_width * $self->grid_height))
106 }
107  
108 2 __PACKAGE__->meta->make_immutable;
109  
110 2 1;
111  
112 #################### pod generated by Pod::Autopod - keep this line to make pod updates possible ####################
113  
114 =head1 NAME
115  
116 Grid - create geometric grids
117  
118 =head1 SYNOPSYS
119  
120 use Grid;
121  
122 my $grid = Grid->new($grid_width, $grid_height, $item_width, $item_height, $gutter, $border, $arrangement);
123  
124 =head1 DESCRIPTION
125  
126 Grid creates an array of x-y positions for items of a given height and wdith arranged in a grid. This is used to create grid layouts on a page, or repeate items on a number of pages of the same size.
127  
128 =head1 REQUIRES
129  
130 L<POSIX>
131  
132 L<List::AllUtils>
133  
134 L<Moose::Util::TypeConstraints>
135  
136 L<Moose>
137  
138  
139 =head1 METHODS
140  
141 =head2 bbox
142  
143 $grid->bbox();
144  
145 Returns the total bounding box of the grid
146  
147 =head2 numbers
148  
149 $grid->numbers();
150  
151 Returns the sequence item numbers, with the top left item as item 1.
152  
153 +---------+---------+---------+---------+
154 | | | | |
155 | 1 | 2 | 3 | 4 |
156 | | | | |
157 +---------+---------+---------+---------+
158 | | | | |
159 | 5 | 6 | 7 | 8 |
160 | | | | |
161 +---------+---------+---------+---------+
162 | | | | |
163 | 9 | 10 | 11 | 12 |
164 | | | | |
165 +---------+---------+---------+---------+
166  
167 =head2 sequence
168  
169 $grid->sequence();
170  
171 Returns the sequence of x-y grid item coordinates, with the top left item as item C<[0, 0]>, the next one (assuming a horizontal arrangement) being C<[1, 0]> etc.
172  
173 +---------+---------+---------+---------+
174 | | | | |
175 | [0, 0] | [0, 1] | [0, 2] | [0, 3] |
176 | | | | |
177 +---------+---------+---------+---------+
178 | | | | |
179 | [1, 0] | [1, 1] | [1, 2] | [1, 3] |
180 | | | | |
181 +---------+---------+---------+---------+
182 | | | | |
183 | [2, 0] | [2, 1] | [2, 2] | [2, 3] |
184 | | | | |
185 +---------+---------+---------+---------+
186  
187 =head2 positions
188  
189 $grid->positions();
190  
191 Returns the sequence of x-y grid coordinates, taking optional offsets. If two offsets are provided, the x-y position is offset accordingly, and if four are provided, it returns a boumding box.
192  
193 =head2 total_height
194  
195 $grid->total_height();
196  
197 The total height of the grid
198  
199 =head2 total_width
200  
201 $grid->total_width();
202  
203 The total width of the grid
204  
205 =head1 To do
206  
207 =over 4
208  
209 =item *
210  
211 Allow for different vertical and horizontal gutters
212  
213 =item *
214  
215 Allow for different top, bottom, left right borders
216  
217 =item *
218  
219 Allow for bottom or top start of grid
220  
221 =back
222  
223 =cut
↑ Summary