OrientedRectangle.pm

filter: Statementstmt 98.3% 118 / 120 Branchbran 79.5% 35 / 44 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 17 / 17 Podpod 100% 7 / 7 totaltotal 94.1% 177 / 188 CC 24 SCAR 16.6
CC
24
Cov
93%
collides
11.1
corner
6.1
get_edge
5.0
CRAP 5.3
« LineSegment.pm ↑ Summary Line.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::OrientedRectangle;
4 6 $Math::Shape::OrientedRectangle::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::Rectangle;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 6 ▶use Math::Shape::Circle;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 # ABSTRACT: a 2d oriented rectangle
15  
16  
17 14 ▶sub new {
Subroutine new: called CC=2 SCAR=7.1
Pod documented
18 14 ▶ croak 'incorrect number of args' unless @_ == 6;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ == 6 0 14 14
19 14 my ($class, $x_1, $y_1, $x_2, $y_2, $rotation) = @_;
20 14 bless { center => Math::Shape::Vector->new($x_1, $y_1),
21 half_extend => Math::Shape::Vector->new($x_2, $y_2),
22 rotation => $rotation,
23 }, $class;
24 }
25  
26  
27 63 ▶sub get_edge {
Subroutine get_edge: called CC=5 SCAR=16.1
Pod documented
28 63 ▶ croak 'incorrect number of args' unless @_ == 2;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ == 2 0 63 63
29 63 my ($self, $edge_number) = @_;
30  
31 my $a = Math::Shape::Vector->new(
32 $self->{half_extend}->{x},
33 63 $self->{half_extend}->{y});
34  
35 my $b = Math::Shape::Vector->new(
36 $self->{half_extend}->{x},
37 63 $self->{half_extend}->{y});
38  
39 63 my $mod = $edge_number % 4;
40  
41 63 ▶ if ($mod == 0)
Branch: 3 branches
BranchTrueFalseTotal
if ($mod == 0) { } 31 32 63
elsif ($mod == 1) { } 13 19 32
elsif ($mod == 2) { } 18 1 19
42 {
43 31 $a->{x} = - $a->{x};
44 }
45 elsif ($mod == 1)
46 {
47 13 $b->{y} = - $b->{y};
48 }
49 elsif ($mod == 2)
50 {
51 18 $a->{y} = - $a->{y};
52 18 $b = $b->negate;
53 }
54 else
55 {
56 1 $a->negate;
57 1 $b->{x} = - $b->{x};
58 }
59  
60 63 $a = $a->rotate($self->{rotation});
61 63 $a = $a->add_vector($self->{center});
62 63 $b = $b->rotate($self->{rotation});
63 63 $b = $b->add_vector($self->{center});
64  
65 63 Math::Shape::LineSegment->new($a->{x}, $a->{y}, $b->{x}, $b->{y});
66 }
67  
68  
69 sub axis_is_separating
70 17 ▶{
Subroutine axis_is_separating: called CC=3 SCAR=11.0
Pod documented
71 17 ▶ croak 'collides must be called with a Math::Shape::LineSegment object'
Branch: 1 branch
BranchTrueFalseTotal
unless $_[1]->isa("Math::Shape::LineSegment") 0 17 17
72 unless $_[1]->isa('Math::Shape::LineSegment');
73 17 my ($self, $axis) = @_;
74  
75 17 my $edge_0 = $self->get_edge(0);
76 17 my $edge_2 = $self->get_edge(2);
77  
78 17 my $n_vector = $axis->{start}->subtract_vector($axis->{end});
79  
80 17 my $axis_range = $axis->project($n_vector);
81 17 my $range_0 = $edge_0->project($n_vector);
82 17 my $range_2 = $edge_2->project($n_vector);
83 17 my $projection = $range_0->hull($range_2);
84  
85 17 ▶ $axis_range->is_overlapping($projection) ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$axis_range->is_overlapping($projection) ? : 16 1 17
86 }
87  
88  
89 sub corner
90 24 ▶{
Subroutine corner: called CC=6 SCAR=18.0
Pod documented
91 24 ▶ croak 'incorrect number of args' unless @_ == 2;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ == 2 0 24 24
92 24 my ($self, $nr) = @_;
93  
94 24 my $mod = $nr % 4;
95 24 my $v;
96  
97 24 ▶ if ($mod == 0)
Branch: 4 branches
BranchTrueFalseTotal
if ($mod == 0) { } 6 18 24
elsif ($mod == 1) { } 6 12 18
elsif ($mod == 2) { } 6 6 12
elsif ($mod == 3) { } 6 0 6
98 {
99 $v = Math::Shape::Vector->new(
100 - $self->{half_extend}{x},
101 $self->{half_extend}{y},
102 6 );
103 }
104 elsif ($mod == 1)
105 {
106 $v = Math::Shape::Vector->new(
107 $self->{half_extend}{x},
108 $self->{half_extend}{y},
109 6 );
110 }
111 elsif ($mod == 2)
112 {
113 $v = Math::Shape::Vector->new(
114 $self->{half_extend}{x},
115 - $self->{half_extend}{y},
116 6 );
117 }
118 elsif ($mod == 3)
119 {
120 $v = Math::Shape::Vector->new(
121 $self->{half_extend}{x},
122 $self->{half_extend}{y},
123 6 );
124 6 $v = $v->negate;
125 }
126 else
127 {
128 0 croak 'corner() should be called with a number between 0-3';
129 }
130 24 my $c = $v->rotate($self->{rotation});
131 24 $c->add_vector($self->{center});
132 }
133  
134  
135  
136 sub hull
137 6 ▶{
Subroutine hull: called CC=2 SCAR=6.9
Pod documented
138 6 my $self = shift;
139  
140 # create a rectangle at the same center point as $self
141 my $h = Math::Shape::Rectangle->new(
142 $self->{center}{x},
143 $self->{center}{y},
144 6 0,
145 0,
146 );
147  
148 # enlarge the rectangle by every corner vector of $self
149 6 for (0..3)
150 {
151 24 my $corner = $self->corner($_);
152 24 $h = $h->enlarge($corner);
153 }
154  
155 # return the hull
156 6 $h;
157 }
158  
159  
160 sub circle_hull
161 1 ▶{
Subroutine circle_hull: called CC=1 SCAR=0.0
Pod documented
162 1 my $self = shift;
163  
164 Math::Shape::Circle->new(
165 $self->{center}->{x},
166 $self->{center}->{y},
167 $self->{half_extend}->length,
168 1 );
169 }
170  
171  
172 27 ▶sub collides {
Subroutine collides: called CC=11 SCAR=24.0
Pod documented
173 27 my ($self, $other_obj) = @_;
174  
175 27 ▶ if ($other_obj->isa('Math::Shape::OrientedRectangle'))
Branch: 6 branches
BranchTrueFalseTotal
if ($other_obj->isa('Math::Shape::OrientedRectangle')) { } 5 22 27
elsif ($other_obj->isa('Math::Shape::Vector')) { } 6 16 22
elsif ($other_obj->isa('Math::Shape::Line')) { } 6 10 16
elsif ($other_obj->isa('Math::Shape::LineSegment')) { } 6 4 10
elsif ($other_obj->isa('Math::Shape::Rectangle')) { } 2 2 4
elsif ($other_obj->isa('Math::Shape::Circle')) { } 2 0 2
176 {
177 5 my $edge = $self->get_edge(0);
178 5 ▶ return 0 if $other_obj->axis_is_separating($edge);
Branch: 1 branch
BranchTrueFalseTotal
if $other_obj->axis_is_separating($edge) 1 4 5
179  
180 4 $edge = $self->get_edge(1);
181 4 ▶ return 0 if $other_obj->axis_is_separating($edge);
Branch: 1 branch
BranchTrueFalseTotal
if $other_obj->axis_is_separating($edge) 0 4 4
182  
183 4 $edge = $other_obj->get_edge(0);
184 4 ▶ return 0 if $self->axis_is_separating($edge);
Branch: 1 branch
BranchTrueFalseTotal
if $self->axis_is_separating($edge) 0 4 4
185  
186 4 $edge = $other_obj->get_edge(1);
187 4 ▶ return 0 if $self->axis_is_separating($edge);
Branch: 1 branch
BranchTrueFalseTotal
if $self->axis_is_separating($edge) 0 4 4
188  
189 4 1;
190 }
191 elsif ($other_obj->isa('Math::Shape::Vector'))
192 {
193 # convert into rectangle and use rectangle's collides() method
194 6 my $size = $self->{half_extend}->multiply(2);
195 my $lr = Math::Shape::Rectangle->new(
196 0,
197 0,
198 $size->{x},
199 $size->{y},
200 6 );
201  
202 6 my $lp = $other_obj->subtract_vector($self->{center});
203 6 $lp = $lp->rotate(- $self->{rotation});
204 6 $lp = $lp->add_vector($self->{half_extend});
205 6 $lr->collides($lp);
206 }
207 elsif ($other_obj->isa('Math::Shape::Line'))
208 {
209 6 my $size = $self->{center}->multiply(2);
210 my $lr = Math::Shape::Rectangle->new(
211 0,
212 0,
213 $size->{x},
214 $size->{y},
215 6 );
216  
217 6 my $base = $other_obj->{base}->subtract_vector($self->{center});
218 6 $base = $base->rotate(- $self->{rotation});
219 6 $base = $base->add_vector($self->{half_extend});
220 6 my $direction = $other_obj->{direction}->rotate(- $self->{rotation});
221  
222 my $ll = Math::Shape::Line->new(
223 $base->{x},
224 $base->{y},
225 $direction->{x},
226 $direction->{y},
227 6 );
228  
229 6 $lr->collides($ll);
230 }
231 elsif ($other_obj->isa('Math::Shape::LineSegment'))
232 {
233 6 my $size = $self->{half_extend}->multiply(2);
234 my $lr = Math::Shape::Rectangle->new(
235 0,
236 0,
237 $size->{x},
238 $size->{y},
239 6 );
240  
241 6 my $ls_p1 = $other_obj->{start}->subtract_vector($self->{center});
242 6 $ls_p1 = $ls_p1->rotate(- $self->{rotation});
243 6 $ls_p1 = $ls_p1->add_vector($self->{half_extend});
244  
245 6 my $ls_p2 = $other_obj->{end}->subtract_vector($self->{center});
246 6 $ls_p2 = $ls_p2->rotate(- $self->{rotation});
247 6 $ls_p2 = $ls_p2->add_vector($self->{half_extend});
248  
249 my $ls = Math::Shape::LineSegment->new(
250 $ls_p1->{x},
251 $ls_p1->{y},
252 $ls_p2->{x},
253 $ls_p2->{y},
254 6 );
255  
256 6 $lr->collides($ls);
257 }
258 # call the other objects collides() method
259 elsif ($other_obj->isa('Math::Shape::Rectangle'))
260 {
261 2 $other_obj->collides($self);
262 }
263 elsif ($other_obj->isa('Math::Shape::Circle'))
264 {
265 2 $other_obj->collides($self);
266 }
267 else
268 {
269 0 croak 'collides must be called with a Math::Shape::Vector library object';
270 }
271 }
272  
273 6 1;
274  
275 __END__
« LineSegment.pm ↑ Summary Line.pm »