| 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::Line; | ||||||||||||||||||||||||||||||
| 4 | 7 | $Math::Shape::Line::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::Vector; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||
| 9 | # ABSTRACT: a 2d vector-based infinite line | ||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||
| 12 | 39 | ▶ | sub new { | ||||||||||||||||||||||||||||
|
Subroutine
new: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||||||||||||||||||||||
| 13 | 39 | ▶ | croak 'incorrect number of args' unless @_ == 5; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 14 | 39 | my ($class, $x1, $y1, $x2, $y2) = @_; | |||||||||||||||||||||||||||||
| 15 | 39 | bless { base => Math::Shape::Vector->new($x1, $y1), | |||||||||||||||||||||||||||||
| 16 | direction => Math::Shape::Vector->new($x2, $y2), | ||||||||||||||||||||||||||||||
| 17 | }, $class; | ||||||||||||||||||||||||||||||
| 18 | } | ||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||
| 21 | sub is_equivalent | ||||||||||||||||||||||||||||||
| 22 | 8 | ▶ | { | ||||||||||||||||||||||||||||
|
Subroutine
is_equivalent: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||
| 23 | 8 | ▶ | croak 'must pass a line object' unless $_[1]->isa('Math::Shape::Line'); | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 24 | 8 | ▶ | unless( $_[0]->{direction}->is_parallel($_[1]->{direction}) ) | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 25 | { | ||||||||||||||||||||||||||||||
| 26 | 3 | 0; | |||||||||||||||||||||||||||||
| 27 | } | ||||||||||||||||||||||||||||||
| 28 | else | ||||||||||||||||||||||||||||||
| 29 | { | ||||||||||||||||||||||||||||||
| 30 | 5 | my $base = $_[0]->{base}->subtract_vector($_[1]->{base}); | |||||||||||||||||||||||||||||
| 31 | 5 | $base->is_parallel($_[0]->{direction}); | |||||||||||||||||||||||||||||
| 32 | } | ||||||||||||||||||||||||||||||
| 33 | } | ||||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||||
| 36 | sub on_one_side | ||||||||||||||||||||||||||||||
| 37 | 20 | ▶ | { | ||||||||||||||||||||||||||||
|
Subroutine
on_one_side: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||||||||||||
| 38 | 20 | ▶ | croak 'project not called with argument of type Math::Shape::Line' unless $_[1]->isa('Math::Shape::LineSegment'); | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 39 | 20 | my ($self, $segment) = @_; | |||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||||
| 41 | 20 | my $vector_d1 = $segment->{start}->subtract_vector($self->{base}); | |||||||||||||||||||||||||||||
| 42 | 20 | my $vector_d2 = $segment->{end}->subtract_vector($self->{base}); | |||||||||||||||||||||||||||||
| 43 | 20 | my $vector_n = $self->{direction}->rotate_90; | |||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||||
| 45 | 20 | ▶ | $vector_n->dot_product($vector_d1) * $vector_n->dot_product($vector_d2) | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 46 | > 0 ? 1 : 0; | ||||||||||||||||||||||||||||||
| 47 | } | ||||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||||
| 51 | sub collides | ||||||||||||||||||||||||||||||
| 52 | 21 | ▶ | { | ||||||||||||||||||||||||||||
|
Subroutine
collides: called CC=9 SCAR=22.0
Pod
documented
| |||||||||||||||||||||||||||||||
| 53 | 21 | my ($self, $other_obj) = @_; | |||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||||
| 55 | 21 | ▶ | if ($other_obj->isa('Math::Shape::Line')) | ||||||||||||||||||||||||||||
|
Branch: 6 branches
| |||||||||||||||||||||||||||||||
| 56 | { | ||||||||||||||||||||||||||||||
| 57 | 7 | ▶ | if($self->{direction}->is_parallel($other_obj->{direction})) | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 58 | { | ||||||||||||||||||||||||||||||
| 59 | 4 | $self->is_equivalent($other_obj); | |||||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||||
| 61 | else | ||||||||||||||||||||||||||||||
| 62 | { | ||||||||||||||||||||||||||||||
| 63 | 3 | 1; | |||||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||||
| 66 | elsif ($other_obj->isa('Math::Shape::LineSegment')) | ||||||||||||||||||||||||||||||
| 67 | { | ||||||||||||||||||||||||||||||
| 68 | 6 | ▶ | $self->on_one_side($other_obj) ? 0 : 1; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||||
| 70 | elsif ($other_obj->isa('Math::Shape::OrientedRectangle')) | ||||||||||||||||||||||||||||||
| 71 | { | ||||||||||||||||||||||||||||||
| 72 | 2 | $other_obj->collides($self); | |||||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||||
| 74 | elsif ($other_obj->isa('Math::Shape::Circle')) | ||||||||||||||||||||||||||||||
| 75 | { | ||||||||||||||||||||||||||||||
| 76 | 2 | $other_obj->collides($self); | |||||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||||
| 78 | elsif ($other_obj->isa('Math::Shape::Rectangle')) | ||||||||||||||||||||||||||||||
| 79 | { | ||||||||||||||||||||||||||||||
| 80 | 2 | $other_obj->collides($self); | |||||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||||
| 82 | elsif ($other_obj->isa('Math::Shape::Vector')) | ||||||||||||||||||||||||||||||
| 83 | { | ||||||||||||||||||||||||||||||
| 84 | 2 | $other_obj->collides($self); | |||||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||||
| 86 | else | ||||||||||||||||||||||||||||||
| 87 | { | ||||||||||||||||||||||||||||||
| 88 | 0 | croak 'collides must be called with a Math::Shape::Vector library object'; | |||||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||||
| 94 | 7 | 1; | |||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||||
| 96 | __END__ | ||||||||||||||||||||||||||||||