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