Circle.pm

filter: Statementstmt 97.9% 48 / 49 Branchbran 87.5% 21 / 24 Conditioncond 66.6% 4 / 6 MC/DCmcdc 33.3% 1 / 3 Subroutinesub 100% 8 / 8 Podpod 100% 2 / 2 totaltotal 91.3% 84 / 92 CC 15 SCAR 22.5
CC
15
Cov
92%
collides
14.1
new
2.0
BEGIN
1.0
CRAP 9.5
« Rectangle.pm ↑ Summary LineSegment.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::Circle;
4 6 $Math::Shape::Circle::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::Rectangle;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 # ABSTRACT: a 2d circle
11  
12  
13 14 ▶sub new {
Subroutine new: called CC=2 SCAR=7.1
Pod documented
14 14 ▶ croak 'incorrect number of args' unless @_ == 4;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ == 4 0 14 14
15 14 my ($class, $x, $y, $r) = @_;
16 14 bless { center => Math::Shape::Vector->new($x, $y),
17 radius => $r,
18 }, $class;
19 }
20  
21  
22 sub collides
23 81 ▶{
Subroutine collides: called CC=14 SCAR=26.5
Pod documented
24 81 my ($self, $other_obj) = @_;
25  
26 81 ▶ if ($other_obj->isa('Math::Shape::Circle'))
Branch: 6 branches
BranchTrueFalseTotal
if ($other_obj->isa('Math::Shape::Circle')) { } 8 73 81
elsif ($other_obj->isa('Math::Shape::Vector')) { } 41 32 73
elsif ($other_obj->isa('Math::Shape::Line')) { } 7 25 32
elsif ($other_obj->isa('Math::Shape::LineSegment')) { } 7 18 25
elsif ($other_obj->isa('Math::Shape::Rectangle')) { } 12 6 18
elsif ($other_obj->isa('Math::Shape::OrientedRectangle')) { } 6 0 6
27 {
28 8 my $center = $self->{center}->subtract_vector($other_obj->{center});
29 8 ▶ $center->length <= $self->{radius} + $other_obj->{radius} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$center->length <= $self->{'radius'} + $other_obj->{'radius'} ? : 4 4 8
30 }
31 elsif ($other_obj->isa('Math::Shape::Vector'))
32 {
33 41 my $center = $self->{center}->subtract_vector($other_obj);
34 41 ▶ $center->length <= $self->{radius} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$center->length <= $self->{'radius'} ? : 18 23 41
35 }
36 elsif ($other_obj->isa('Math::Shape::Line'))
37 {
38 7 my $center = $self->{center}->subtract_vector($other_obj->{base});
39 7 my $project = $center->project($other_obj->{direction});
40  
41 7 my $base_vector = $other_obj->{base}->add_vector($project);
42 7 $self->collides($base_vector);
43 }
44 elsif ($other_obj->isa('Math::Shape::LineSegment'))
45 {
46 # test collision of both LineSegment start/end points
47 7 ▶ return 1 if $self->collides($other_obj->{start});
Branch: 1 branch
BranchTrueFalseTotal
if $self->collides($other_obj->{"start"}) 3 4 7
48 4 ▶ return 1 if $self->collides($other_obj->{end});
Branch: 1 branch
BranchTrueFalseTotal
if $self->collides($other_obj->{"end"}) 0 4 4
49  
50 # test collision of nearest point on LineSegment with circle
51 4 my $d = $other_obj->{end}->subtract_vector($other_obj->{start});
52 4 my $lc = $self->{center}->subtract_vector($other_obj->{start});
53 4 my $p = $lc->project($d);
54 4 my $nearest = $other_obj->{start}->add_vector($p);
55  
56 4 ▶ $self->collides($nearest)
Branch: 1 branch
BranchTrueFalseTotal
$self->collides($nearest) && $p->length <= $d->length && 0 <= $p->dot_product($d) ? : 1 3 4
Condition66%4/6
$self->collides($nearest) and $p->length <= $d->length and 0 <= $p->dot_product($d)
!ll&&!rl&&r
301
$self->collides($nearest) and $p->length <= $d->length
!ll&&!rl&&r
301
MC/DC33%1/3
$self->collides($nearest) and $p->length <= $d->length and 0 <= $p->dot_product($d)
$self->collides($nearest) $p->length <= $d->length 0 <= $p->dot_product($d)
Truth table50%2/4
A and B and C
A= $self->collides($nearest)B= $p->length <= $d->lengthC= 0 <= $p->dot_product($d)
ABCresult
0XX0
10X0
1100
1111
57 && $p->length <= $d->length
58 && 0 <= $p->dot_product($d)
59 ? 1 : 0;
60 }
61 elsif ($other_obj->isa('Math::Shape::Rectangle'))
62 {
63 12 my $clamped_vector = $other_obj->clamp($self->{center});
64  
65 12 $self->collides($clamped_vector);
66 }
67 elsif ($other_obj->isa('Math::Shape::OrientedRectangle'))
68 {
69 # transform OrientedRectangle into Rectangle
70 6 my $r_size = $other_obj->{half_extend}->multiply(2);
71 6 my $lr = Math::Shape::Rectangle->new(0, 0, $r_size->{x}, $r_size->{y});
72  
73 # transform $self into local Circle in coordinates of other_obj
74 6 my $distance = $self->{center}->subtract_vector($other_obj->{center});
75 6 $distance = $distance->rotate(- $other_obj->{rotation});
76 6 my $center = $distance->add_vector($other_obj->{half_extend});
77 6 my $lc = Math::Shape::Circle->new($center->{x}, $center->{y}, $self->{radius});
78  
79 # check local objects collide
80 6 $lc->collides($lr);
81 }
82 else
83 {
84 0 croak 'collides must be called with a Math::Shape::Vector library object';
85 }
86 }
87 6 1;
88  
89 __END__
« Rectangle.pm ↑ Summary LineSegment.pm »