Vector.pm

filter: Statementstmt 96.0% 98 / 102 Branchbran 82.2% 51 / 62 Conditioncond 83.3% 10 / 12 MC/DCmcdc 71.4% 5 / 7 Subroutinesub 100% 27 / 27 Podpod 95.0% 19 / 20 totaltotal 91.3% 210 / 230 CC 37 SCAR 15.5
CC
37
Cov
90%
collides
13.2
distance
5.6
is_equal
4.0
CRAP 4.7
« Line.pm ↑ Summary Utils.pm »
1 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 7 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 package Math::Shape::Vector;
4 7 $Math::Shape::Vector::VERSION = '0.15';
5 7 ▶use 5.008;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 7 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 7 ▶use Math::Shape::Utils;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 7 ▶use Math::Trig qw/acos :pi/;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 # ABSTRACT: A 2d vector library in cartesian space
11  
12  
13 1770 ▶sub new {
Subroutine new: called CC=2 SCAR=6.9
Pod documented
14 1770 ▶ croak 'incorrect number of arguments' unless @_ == 3;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ == 3 2 1768 1770
15 1768 return bless { x => $_[1],
16 y => $_[2] }, $_[0];
17 }
18  
19  
20 226 ▶sub add_vector {
Subroutine add_vector: called CC=2 SCAR=6.9
Pod documented
21 226 ▶ croak 'must pass a vector object' unless $_[1]->isa('Math::Shape::Vector');
Branch: 1 branch
BranchTrueFalseTotal
unless $_[1]->isa("Math::Shape::Vector") 1 224 225
22 224 my ($self, $v2) = @_;
23  
24 Math::Shape::Vector->new(
25 $self->{x} + $v2->{x},
26 $self->{y} + $v2->{y},
27 224 );
28 }
29  
30  
31 331 ▶sub subtract_vector {
Subroutine subtract_vector: called CC=2 SCAR=6.9
Pod documented
32 331 ▶ croak 'must pass a vector object' unless $_[1]->isa('Math::Shape::Vector');
Branch: 1 branch
BranchTrueFalseTotal
unless $_[1]->isa("Math::Shape::Vector") 1 328 329
33 328 my ($self, $v2) = @_;
34  
35 Math::Shape::Vector->new(
36 $self->{x} - $v2->{x},
37 $self->{y} - $v2->{y},
38 328 );
39 }
40  
41  
42 27 ▶sub negate {
Subroutine negate: called CC=1 SCAR=0.0
Pod documented
43 27 my $self = shift;
44  
45 Math::Shape::Vector->new(
46 - $self->{x},
47 - $self->{y},
48 27 );
49 }
50  
51  
52 12 ▶sub is_equal {
Subroutine is_equal: called CC=4 SCAR=13.9
Pod documented
53 12 ▶ croak 'must pass a vector object' unless $_[1]->isa('Math::Shape::Vector');
Branch: 1 branch
BranchTrueFalseTotal
unless $_[1]->isa("Math::Shape::Vector") 1 11 12
54 11 my ($self, $v2) = @_;
55 $self->{x} == $v2->{x} && $self->{y} == $v2->{y}
56 11 ▶ ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->{'x'} == $v2->{'x'} && $self->{'y'} == $v2->{'y'} ? : 6 5 11
Condition100%3/3
$self->{"x"} == $v2->{"x"} and $self->{"y"} == $v2->{"y"}
!ll&&!rl&&r
326
MC/DC100%2/2
$self->{"x"} == $v2->{"x"} and $self->{"y"} == $v2->{"y"}
$self->{"x"} == $v2->{"x"} $self->{"y"} == $v2->{"y"}
Truth table100%3/3
A and B
A= $self->{"x"} == $v2->{"x"}B= $self->{"y"} == $v2->{"y"}
ABresult
0X0
100
111
57 }
58  
59  
60 42 ▶sub multiply {
Subroutine multiply: called CC=2 SCAR=6.9
Pod documented
61 42 ▶ croak 'incorrect number of args' unless @_ == 2;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ == 2 1 41 42
62 41 my ($self, $multiplier) = @_;
63  
64 Math::Shape::Vector->new(
65 $self->{x} * $multiplier,
66 41 $self->{y} * $multiplier,
67 );
68 }
69  
70  
71 97 ▶sub divide {
Subroutine divide: called CC=4 SCAR=13.9
Pod documented
72 97 ▶ croak 'incorrect number of args' unless @_ == 2;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ == 2 1 96 97
73 96 my ($self, $divisor) = @_;
74  
75 # avoid division by zero
76 Math::Shape::Vector->new(
77 ($divisor ? $self->{x} / $divisor : 0),
78 96 ▶ ($divisor ? $self->{y} / $divisor : 0),
Branch: 2 branches
BranchTrueFalseTotal
$divisor ? : 95 1 96
$divisor ? : 95 1 96
79 );
80 }
81  
82  
83 189 ▶sub rotate {
Subroutine rotate: called CC=2 SCAR=7.1
Pod documented
84 189 ▶ croak 'incorrect number of args' unless @_ == 2;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ == 2 0 189 189
85 189 my ($self, $radians) = @_;
86  
87 Math::Shape::Vector->new(
88 $self->{x} * cos($radians) - $self->{y} * sin($radians),
89 189 $self->{x} * sin($radians) + $self->{y} * cos($radians),
90 );
91 }
92  
93  
94 sub rotate_90
95 70 ▶{
Subroutine rotate_90: called CC=1 SCAR=0.0
Pod documented
96 70 my $self = shift;
97  
98 Math::Shape::Vector->new(
99 - $self->{y},
100 $self->{x},
101 70 );
102 }
103  
104  
105 367 ▶sub dot_product {
Subroutine dot_product: called CC=2 SCAR=7.1
Pod documented
106 367 ▶ croak 'must pass a vector object' unless $_[1]->isa('Math::Shape::Vector');
Branch: 1 branch
BranchTrueFalseTotal
unless $_[1]->isa("Math::Shape::Vector") 0 367 367
107 367 my ($self, $v2) = @_;
108 367 $self->{x} * $v2->{x} + $self->{y} * $v2->{y};
109 }
110  
111  
112 163 ▶sub length {
Subroutine length: called CC=2 SCAR=6.9
Pod documented
113 163 my $self = shift;
114 # avoid division by zero for null vectors
115 163 my $sum_of_squares = $self->{x} ** 2 + $self->{y} ** 2;
116  
117 163 ▶ return 0 unless $sum_of_squares;
Branch: 1 branch
BranchTrueFalseTotal
unless $sum_of_squares 25 138 163
118 138 sqrt $sum_of_squares;
119 }
120  
121  
122 95 ▶sub convert_to_unit_vector {
Subroutine convert_to_unit_vector: called CC=2 SCAR=7.0
Pod documented
123 95 my $self = shift;
124  
125 95 my $length = $self->length;
126  
127 # if the vector length is zero (or lower?) return self
128 95 ▶ return $self if $length < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $length < 0 0 95 95
129  
130 # else return unit vector
131 95 $self->divide($length);
132 }
133  
134  
135 19 ▶sub project {
Subroutine project: called CC=3 SCAR=11.0
Pod documented
136 19 ▶ croak 'must pass a vector object' unless $_[1]->isa('Math::Shape::Vector');
Branch: 1 branch
BranchTrueFalseTotal
unless $_[1]->isa("Math::Shape::Vector") 0 19 19
137 19 my ($self, $v2) = @_;
138  
139 19 my $d = $v2->dot_product($v2);
140  
141 19 ▶ if ($d > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($d > 0) { } 16 3 19
142 16 $v2->multiply( $self->dot_product($v2) / $d );
143 }
144 else {
145 3 $v2;
146 }
147 }
148  
149  
150 sub is_parallel
151 25 ▶{
Subroutine is_parallel: called CC=2 SCAR=7.0
Pod documented
152 25 ▶ croak 'must pass a vector object' unless $_[1]->isa('Math::Shape::Vector');
Branch: 1 branch
BranchTrueFalseTotal
unless $_[1]->isa("Math::Shape::Vector") 0 25 25
153 25 my ($self, $v2) = @_;
154 25 my $vector_na = $self->rotate_90;
155 25 equal_floats(0, $vector_na->dot_product($v2));
156 }
157  
158  
159 sub enclosed_angle
160 4 ▶{
Subroutine enclosed_angle: called CC=2 SCAR=7.0
Pod documented
161 4 ▶ croak 'must pass a vector object' unless $_[1]->isa('Math::Shape::Vector');
Branch: 1 branch
BranchTrueFalseTotal
unless $_[1]->isa("Math::Shape::Vector") 0 4 4
162 4 my ($self, $v2) = @_;
163  
164 4 my $ua = $self->convert_to_unit_vector;
165 4 my $ub = $v2->convert_to_unit_vector;
166  
167 4 acos( $ua->dot_product($ub) );
168 }
169  
170  
171 sub radians
172 6 ▶{
Subroutine radians: called CC=2 SCAR=6.9
Pod documented
173 6 my $radians = atan2 $_[0]->{x}, $_[0]->{y};
174 # if less than zero
175 6 ▶ $radians < 0 ? pi2 + $radians : $radians;
Branch: 1 branch
BranchTrueFalseTotal
$radians < 0 ? : 2 4 6
176 }
177  
178  
179 sub header_vector
180 4 ▶{
Subroutine header_vector: called CC=2 SCAR=7.2
Pod documented
181 4 ▶ croak 'must pass a vector object' unless $_[1]->isa('Math::Shape::Vector');
Branch: 1 branch
BranchTrueFalseTotal
unless $_[1]->isa("Math::Shape::Vector") 0 4 4
182 4 $_[1]->subtract_vector($_[0])->convert_to_unit_vector;
183 }
184  
185  
186 sub collides
187 27 ▶{
Subroutine collides: called CC=13 SCAR=25.8
Pod documented
188 27 my ($self, $other_obj) = @_;
189  
190 27 ▶ if ($other_obj->isa('Math::Shape::Vector'))
Branch: 6 branches
BranchTrueFalseTotal
if ($other_obj->isa('Math::Shape::Vector')) { } 9 18 27
elsif ($other_obj->isa('Math::Shape::LineSegment')) { } 6 12 18
elsif ($other_obj->isa('Math::Shape::Line')) { } 4 8 12
elsif ($other_obj->isa('Math::Shape::OrientedRectangle')) { } 4 4 8
elsif ($other_obj->isa('Math::Shape::Circle')) { } 2 2 4
elsif ($other_obj->isa('Math::Shape::Rectangle')) { } 2 0 2
191 {
192 9 ▶ $self->{x} == $other_obj->{x} && $self->{y} == $other_obj->{y} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->{'x'} == $other_obj->{'x'} && $self->{'y'} == $other_obj->{'y'} ? : 5 4 9
Condition100%3/3
$self->{"x"} == $other_obj->{"x"} and $self->{"y"} == $other_obj->{"y"}
!ll&&!rl&&r
225
MC/DC100%2/2
$self->{"x"} == $other_obj->{"x"} and $self->{"y"} == $other_obj->{"y"}
$self->{"x"} == $other_obj->{"x"} $self->{"y"} == $other_obj->{"y"}
Truth table100%3/3
A and B
A= $self->{"x"} == $other_obj->{"x"}B= $self->{"y"} == $other_obj->{"y"}
ABresult
0X0
100
111
193 }
194 elsif ($other_obj->isa('Math::Shape::LineSegment'))
195 {
196 # test collision of nearest point on LineSegment with vector
197 6 my $d = $other_obj->{end}->subtract_vector($other_obj->{start});
198 6 my $lp = $self->subtract_vector($other_obj->{start});
199 6 my $pr = $lp->project($d);
200  
201 6 ▶ $lp->is_equal($pr)
Branch: 1 branch
BranchTrueFalseTotal
$lp->is_equal($pr) && $pr->length <= $d->length && 0 <= $pr->dot_product($d) ? : 3 3 6
Condition66%4/6
$lp->is_equal($pr) and $pr->length <= $d->length and 0 <= $pr->dot_product($d)
!ll&&!rl&&r
303
$lp->is_equal($pr) and $pr->length <= $d->length
!ll&&!rl&&r
303
MC/DC33%1/3
$lp->is_equal($pr) and $pr->length <= $d->length and 0 <= $pr->dot_product($d)
$lp->is_equal($pr) $pr->length <= $d->length 0 <= $pr->dot_product($d)
Truth table50%2/4
A and B and C
A= $lp->is_equal($pr)B= $pr->length <= $d->lengthC= 0 <= $pr->dot_product($d)
ABCresult
0XX0
10X0
1100
1111
202 && $pr->length <= $d->length
203 && 0 <= $pr->dot_product($d)
204 ? 1 : 0;
205 }
206 elsif ($other_obj->isa('Math::Shape::Line'))
207 {
208 # test if vector collides with base
209 4 ▶ return 1 if $self->collides($other_obj->{base});
Branch: 1 branch
BranchTrueFalseTotal
if $self->collides($other_obj->{"base"}) 2 2 4
210  
211 # test if vector lies on the direction
212 2 my $lp = $self->subtract_vector($other_obj->{base});
213 2 $lp->is_parallel($other_obj->{direction});
214 }
215 elsif ($other_obj->isa('Math::Shape::OrientedRectangle'))
216 {
217 4 $other_obj->collides($self);
218 }
219 elsif ($other_obj->isa('Math::Shape::Circle'))
220 {
221 2 $other_obj->collides($self);
222 }
223 elsif ($other_obj->isa('Math::Shape::Rectangle'))
224 {
225 2 $other_obj->collides($self);
226 }
227 else
228 {
229 0 croak 'collides must be called with a Math::Shape::Vector library object';
230 }
231 }
232  
233  
234 sub distance
235 4 ▶{
Subroutine distance: called CC=4 SCAR=17.2
Pod documented
236 4 my ($self, $other_obj) = @_;
237  
238 4 ▶ if ($other_obj->isa('Math::Shape::Vector'))
Branch: 3 branches
BranchTrueFalseTotal
if ($other_obj->isa('Math::Shape::Vector')) { } 3 1 4
elsif ($other_obj->isa('Math::Shape::Circle')) { } 1 0 1
elsif ($other_obj->isa('Math::Shape::OrientedRectangle')) { } 0 0 0
239 {
240 3 $self->subtract_vector($other_obj)->length;
241 }
242 elsif ($other_obj->isa('Math::Shape::Circle'))
243 {
244 $self->subtract_vector($other_obj->{center})->length
245 1 - $other_obj->{radius};
246 }
247 elsif ($other_obj->isa('Math::Shape::OrientedRectangle'))
248 {
249 0 my $circle_hull = $other_obj->circle_hull;
250 $self->subtract_vector($circle_hull->{center})->length
251 0 - $circle_hull->{radius};
252 }
253 else
254 {
255 0 croak 'distance() must be called with a Math::Shape::Vector library object';
256 }
257 }
258  
259 use overload
260 7 ▶ '""' => \&stringify;
Subroutine BEGIN: called CC=1 SCAR=0.0
261  
262 sub stringify
263 45 ▶{
Subroutine stringify: called CC=2 SCAR=6.9
Pod undocumented
264 45 my $string = 'Vector ';
265 45 for (sort keys %{$_[0]})
266 {
267 90 $string .= "$_: $_[0]->{$_}, ";
268 }
269 45 substr $string, 0, -2;
270 }
271  
272  
273 7 1;
274  
275 __END__
« Line.pm ↑ Summary Utils.pm »