Rectangle.pm

filter: Statementstmt 98.3% 116 / 118 Branchbran 80.0% 40 / 50 Conditioncond 86.6% 13 / 15 MC/DCmcdc 71.4% 5 / 7 Subroutinesub 100% 15 / 15 Podpod 100% 6 / 6 totaltotal 92.4% 195 / 211 CC 31 SCAR 24.3
CC
31
Cov
92%
collides
21.2
corner
6.1
separating_axis
3.0
CRAP 11.4
↑ Summary Circle.pm »
1 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 package Math::Shape::Rectangle;
4 6 $Math::Shape::Rectangle::VERSION = '0.15';
5 6 ▶use 5.008;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 6 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 6 ▶use Math::Shape::Vector;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 6 ▶use Math::Shape::Utils;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 6 ▶use Math::Shape::Line;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 6 ▶use Math::Shape::LineSegment;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 6 ▶use Math::Shape::Range;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 # ABSTRACT: an axis-aligned 2d rectangle
14  
15  
16 62 ▶sub new {
Subroutine new: called CC=2 SCAR=7.1
Pod documented
17 62 ▶ croak 'incorrect number of args' unless @_ == 5;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ == 5 0 62 62
18 62 my ($class, $x, $y, $l, $h) = @_;
19 62 bless { origin => Math::Shape::Vector->new($x, $y),
20 size => Math::Shape::Vector->new($l, $h),
21 }, $class;
22 }
23  
24  
25 sub clamp
26 12 ▶{
Subroutine clamp: called CC=2 SCAR=7.0
Pod documented
27 12 ▶ croak 'clamp must be called with a Math::Shape::Vector object' unless $_[1]->isa('Math::Shape::Vector');
Branch: 1 branch
BranchTrueFalseTotal
unless $_[1]->isa("Math::Shape::Vector") 0 12 12
28 12 my ($self, $vector) = @_;
29  
30 12 my $clamp_x = clamp_on_range($vector->{x}, $self->{origin}->{x}, $self->{origin}->{x} + $self->{size}->{x});
31 12 my $clamp_y = clamp_on_range($vector->{y}, $self->{origin}->{y}, $self->{origin}->{y} + $self->{size}->{y});
32 12 Math::Shape::Vector->new($clamp_x, $clamp_y);
33 }
34  
35  
36 sub corner
37 32 ▶{
Subroutine corner: called CC=6 SCAR=18.1
Pod documented
38 32 ▶ croak 'Incorrect number of arguments for corner(). Requires a number between 0 and 3.' unless @_ == 2;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ == 2 0 32 32
39 32 my ($self, $nr) = @_;
40  
41 32 my $corner;
42 32 my $mod = $nr % 4;
43  
44 32 ▶ if ($mod == 0)
Branch: 4 branches
BranchTrueFalseTotal
if ($mod == 0) { } 8 24 32
elsif ($mod == 1) { } 8 16 24
elsif ($mod == 2) { } 8 8 16
elsif ($mod == 3) { } 8 0 8
45 {
46 $corner = Math::Shape::Vector->new(
47 $self->{origin}{x} + $self->{size}{x},
48 $self->{origin}{y},
49 8 );
50 }
51 elsif ($mod == 1)
52 {
53 $corner = Math::Shape::Vector->new(
54 $self->{origin}{x},,
55 $self->{origin}{y},
56 8 );
57 8 $corner->add_vector($self->{size});
58 }
59 elsif ($mod == 2)
60 {
61 $corner = Math::Shape::Vector->new(
62 $self->{origin}{x},
63 $self->{origin}{y} + $self->{size}{y},
64 8 );
65 }
66 elsif ($mod == 3)
67 {
68 $corner = Math::Shape::Vector->new(
69 $self->{origin}{x},
70 $self->{origin}{y},
71 8 );
72 }
73 else
74 {
75 0 croak 'corner() not called with a number between 0 and 3';
76 }
77 }
78  
79  
80 sub separating_axis
81 8 ▶{
Subroutine separating_axis: called CC=3 SCAR=11.0
Pod documented
82 8 ▶ croak 'separating_axis() requires a Math::Shape::LineSegment object as an argument' unless $_[1]->isa('Math::Shape::LineSegment');
Branch: 1 branch
BranchTrueFalseTotal
unless $_[1]->isa("Math::Shape::LineSegment") 0 8 8
83 8 my ($self, $axis) = @_;
84  
85 8 my $n = $axis->{start}->subtract_vector($axis->{end});
86 8 my $point0 = $self->corner(0);
87 8 my $point1 = $self->corner(1);
88 8 my $point2 = $self->corner(2);
89 8 my $point3 = $self->corner(3);
90  
91 my $r_edge_a = Math::Shape::LineSegment->new(
92 $point0->{x},
93 $point0->{y},
94 $point1->{x},
95 $point1->{y},
96 8 );
97 8 my $r_edge_range_a = $r_edge_a->project($n);
98 my $r_edge_b = Math::Shape::LineSegment->new(
99 $point2->{x},
100 $point2->{y},
101 $point3->{x},
102 $point3->{y},
103 8 );
104 8 my $r_edge_range_b = $r_edge_b->project($n);
105 8 my $r_projection = $r_edge_range_a->hull($r_edge_range_b);
106 8 my $axis_range = $axis->project($n);
107 8 ▶ $axis_range->is_overlapping($r_projection)
Branch: 1 branch
BranchTrueFalseTotal
$axis_range->is_overlapping($r_projection) ? : 7 1 8
108 ? 0 : 1;
109 }
110  
111  
112 sub enlarge
113 24 ▶{
Subroutine enlarge: called CC=2 SCAR=7.0
Pod documented
114 24 ▶ croak 'enlarge() must be called with a Math::Shape::Vector object' unless $_[1]->isa('Math::Shape::Vector');
Branch: 1 branch
BranchTrueFalseTotal
unless $_[1]->isa("Math::Shape::Vector") 0 24 24
115 24 my ($self, $v) = @_;
116  
117 my $size = Math::Shape::Vector->new(
118 maximum($self->{origin}{x} + $self->{size}{x}, $v->{x}),
119 24 maximum($self->{origin}{y} + $self->{size}{y}, $v->{y}),
120 );
121  
122 my $origin = Math::Shape::Vector->new(
123 minimum($self->{origin}{x}, $v->{x}),
124 24 minimum($self->{origin}{y}, $v->{y}),
125 );
126 24 my $enlarged_size = $size->subtract_vector($origin);
127  
128 Math::Shape::Rectangle->new(
129 $origin->{x},
130 $origin->{y},
131 $enlarged_size->{x},
132 $enlarged_size->{y},
133 24 );
134 }
135  
136  
137 70 ▶sub collides {
Subroutine collides: called CC=21 SCAR=30.5
Pod documented
138 70 my ($self, $other_obj) = @_;
139  
140 70 ▶ if ($other_obj->isa('Math::Shape::Rectangle'))
Branch: 6 branches
BranchTrueFalseTotal
if ($other_obj->isa('Math::Shape::Rectangle')) { } 14 56 70
elsif ($other_obj->isa('Math::Shape::Vector')) { } 12 44 56
elsif ($other_obj->isa('Math::Shape::Line')) { } 24 20 44
elsif ($other_obj->isa('Math::Shape::LineSegment')) { } 12 8 20
elsif ($other_obj->isa('Math::Shape::OrientedRectangle')) { } 6 2 8
elsif ($other_obj->isa('Math::Shape::Circle')) { } 2 0 2
141 {
142 14 my $a_left = $self->{origin}{x};
143 14 my $a_right = $a_left + $self->{size}{x};
144 14 my $b_left = $other_obj->{origin}->{x};
145 14 my $b_right = $b_left + $other_obj->{size}{x};
146 14 my $a_bottom = $self->{origin}{y};
147 14 my $a_top = $a_bottom + $self->{size}{y};
148 14 my $b_bottom = $other_obj->{origin}{y};
149 14 my $b_top = $b_bottom + $other_obj->{size}{y};
150  
151 # overlap returns 1 / 0 already, so no need to use ternary to force 1/0 response
152 14 ▶ overlap($a_left, $a_right, $b_left, $b_right)
Branch: 1 branch
BranchTrueFalseTotal
overlap($a_left, $a_right, $b_left, $b_right) and overlap($a_bottom, $a_top, $b_bottom, $b_top) 12 2 14
153 && overlap($a_bottom, $a_top, $b_bottom, $b_top);
154 }
155 elsif ($other_obj->isa('Math::Shape::Vector'))
156 {
157 12 my $left = $self->{origin}{x};
158 12 my $right = $left + $self->{size}{x};
159 12 my $bottom = $self->{origin}{y};
160 12 my $top = $bottom + $self->{size}{y};
161  
162 # use ternary here as Perl will return undef if false, but we need 0
163 $left <= $other_obj->{x}
164 && $bottom <= $other_obj->{y}
165 && $other_obj->{x} <= $right
166 12 ▶ && $other_obj->{y} <= $top
Branch: 1 branch
BranchTrueFalseTotal
$left <= $other_obj->{'x'} && $bottom <= $other_obj->{'y'} && $other_obj->{'x'} <= $right && $other_obj->{'y'} <= $top ? : 6 6 12
Condition100%9/9
$left <= $other_obj->{"x"} and $bottom <= $other_obj->{"y"} and $other_obj->{"x"} <= $right and $other_obj->{"y"} <= $top
!ll&&!rl&&r
516
$left <= $other_obj->{"x"} and $bottom <= $other_obj->{"y"} and $other_obj->{"x"} <= $right
!ll&&!rl&&r
417
$left <= $other_obj->{"x"} and $bottom <= $other_obj->{"y"}
!ll&&!rl&&r
228
MC/DC100%4/4
$left <= $other_obj->{"x"} and $bottom <= $other_obj->{"y"} and $other_obj->{"x"} <= $right and $other_obj->{"y"} <= $top
$left <= $other_obj->{"x"} $bottom <= $other_obj->{"y"} $other_obj->{"x"} <= $right $other_obj->{"y"} <= $top
Truth table100%5/5
A and B and C and D
A= $left <= $other_obj->{"x"}B= $bottom <= $other_obj->{"y"}C= $other_obj->{"x"} <= $rightD= $other_obj->{"y"} <= $top
ABCDresult
0XXX0
10XX0
110X0
11100
11111
167 ? 1 : 0;
168 }
169 elsif ($other_obj->isa('Math::Shape::Line'))
170 {
171 24 my $n = $other_obj->{direction}->rotate_90;
172 24 my $c1 = $self->{origin};
173 24 my $c2 = $c1->add_vector($self->{size});
174 24 my $c3 = Math::Shape::Vector->new($c2->{x}, $c1->{y});
175 24 my $c4 = Math::Shape::Vector->new($c1->{x}, $c2->{y});
176 24 $c1 = $c1->subtract_vector($other_obj->{base});
177 24 $c2 = $c2->subtract_vector($other_obj->{base});
178 24 $c3 = $c3->subtract_vector($other_obj->{base});
179 24 $c4 = $c4->subtract_vector($other_obj->{base});
180  
181 24 my $dp1 = $n->dot_product($c1);
182 24 my $dp2 = $n->dot_product($c2);
183 24 my $dp3 = $n->dot_product($c3);
184 24 my $dp4 = $n->dot_product($c4);
185  
186 # use ternary here as Perl will return undef if false, but we need 0
187 24 ▶ ($dp1 * $dp2 <= 0)
Branch: 1 branch
BranchTrueFalseTotal
$dp1 * $dp2 <= 0 || $dp2 * $dp3 <= 0 || $dp3 * $dp4 <= 0 ? : 12 12 24
Condition66%4/6
$dp1 * $dp2 <= 0 or $dp2 * $dp3 <= 0 or $dp3 * $dp4 <= 0
l!l&&r!l&&!r
12012
$dp1 * $dp2 <= 0 or $dp2 * $dp3 <= 0
l!l&&r!l&&!r
12012
MC/DC33%1/3
$dp1 * $dp2 <= 0 or $dp2 * $dp3 <= 0 or $dp3 * $dp4 <= 0
$dp1 * $dp2 <= 0 $dp2 * $dp3 <= 0 $dp3 * $dp4 <= 0
Truth table50%2/4
A or B or C
A= $dp1 * $dp2 <= 0B= $dp2 * $dp3 <= 0C= $dp3 * $dp4 <= 0
ABCresult
1XX1
01X1
0011
0000
188 || ($dp2 * $dp3 <= 0)
189 || ($dp3 * $dp4 <= 0)
190 ? 1 : 0;
191 }
192 elsif ($other_obj->isa('Math::Shape::LineSegment'))
193 {
194 # convert LineSegment into an infinite line and test for collision
195 12 my $base = $other_obj->{start};
196 12 my $direction = $other_obj->{end}->subtract_vector($other_obj->{start});
197 12 my $s_line = Math::Shape::Line->new($base->{x}, $base->{y}, $direction->{x}, $direction->{y});
198 12 ▶ return 0 unless $self->collides($s_line);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->collides($s_line) 6 6 12
199  
200 # convert both objects to ranges and check for overlap along x axis
201 my $r_range_x = Math::Shape::Range->new(
202 $self->{origin}{x},
203 $self->{origin}{x} + $self->{size}{x},
204 6 );
205 my $s_range_x = Math::Shape::Range->new(
206 $other_obj->{start}{x},
207 $other_obj->{end}{x},
208 6 );
209 6 $s_range_x = $s_range_x->sort;
210 6 ▶ return 0 unless $s_range_x->is_overlapping($r_range_x);
Branch: 1 branch
BranchTrueFalseTotal
unless $s_range_x->is_overlapping($r_range_x) 0 6 6
211  
212 # convert both objects to ranges and check for overlap along y axis
213 my $r_range_y = Math::Shape::Range->new(
214 $self->{origin}{y},
215 $self->{origin}{y} + $self->{size}{y},
216 6 );
217 my $s_range_y = Math::Shape::Range->new(
218 $other_obj->{start}{y},
219 $other_obj->{end}{y},
220 6 );
221 6 $s_range_y = $s_range_y->sort;
222 6 ▶ return 0 unless $s_range_y->is_overlapping($r_range_y);
Branch: 1 branch
BranchTrueFalseTotal
unless $s_range_y->is_overlapping($r_range_y) 0 6 6
223 }
224 elsif ($other_obj->isa('Math::Shape::OrientedRectangle'))
225 {
226 # get rectangular hull of oriented rectangle
227 # if no collision with hull, we're good
228 6 my $or_hull = $other_obj->hull;
229 6 ▶ return 0 unless $self->collides($or_hull);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->collides($or_hull) 2 4 6
230  
231 # if oriented rectangle edge 0 is a separating axis, we're good
232 4 my $or_edge_0 = $other_obj->get_edge(0);
233 4 ▶ return 0 if $self->separating_axis($or_edge_0);
Branch: 1 branch
BranchTrueFalseTotal
if $self->separating_axis($or_edge_0) 0 4 4
234  
235 # if oriented rectangle edge 1 is a separating axis, we're good
236 4 my $or_edge_1 = $other_obj->get_edge(1);
237 4 ▶ return 0 if $self->separating_axis($or_edge_1);
Branch: 1 branch
BranchTrueFalseTotal
if $self->separating_axis($or_edge_1) 1 3 4
238  
239 # must be collision
240 3 1;
241 }
242 elsif ($other_obj->isa('Math::Shape::Circle'))
243 {
244 # if it's a circle use the circle's collision method
245 2 $other_obj->collides($self);
246 }
247 else
248 {
249 0 croak 'collides must be called with a Math::Shape::Vector library object';
250 }
251 }
252  
253 6 1;
254  
255 __END__
↑ Summary Circle.pm »