| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 48 | 4 | ▶ | return 1 if $self->collides($other_obj->{end}); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition66%4/6
$self->collides($nearest) and $p->length <= $d->length and 0 <= $p->dot_product($d)
$self->collides($nearest) and $p->length <= $d->length
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)
| |||||||||||||||||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||||||||||||||||