line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
7
|
|
|
7
|
|
18006
|
use strict; |
|
7
|
|
|
|
|
10
|
|
|
7
|
|
|
|
|
224
|
|
2
|
7
|
|
|
7
|
|
28
|
use warnings; |
|
7
|
|
|
|
|
8
|
|
|
7
|
|
|
|
|
289
|
|
3
|
|
|
|
|
|
|
package Math::Shape::LineSegment; |
4
|
|
|
|
|
|
|
$Math::Shape::LineSegment::VERSION = '0.14'; |
5
|
7
|
|
|
7
|
|
112
|
use 5.008; |
|
7
|
|
|
|
|
18
|
|
|
7
|
|
|
|
|
214
|
|
6
|
7
|
|
|
7
|
|
26
|
use Carp; |
|
7
|
|
|
|
|
6
|
|
|
7
|
|
|
|
|
394
|
|
7
|
7
|
|
|
7
|
|
462
|
use Math::Shape::Vector; |
|
7
|
|
|
|
|
12
|
|
|
7
|
|
|
|
|
157
|
|
8
|
7
|
|
|
7
|
|
2569
|
use Math::Shape::Range; |
|
7
|
|
|
|
|
13
|
|
|
7
|
|
|
|
|
161
|
|
9
|
7
|
|
|
7
|
|
1325
|
use Math::Shape::Line; |
|
7
|
|
|
|
|
7
|
|
|
7
|
|
|
|
|
2398
|
|
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
# ABSTRACT: a 2d vector line segment; a line with start and end points |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
sub new { |
15
|
102
|
50
|
|
102
|
1
|
241
|
croak 'incorrect number of args' unless @_ == 5; |
16
|
102
|
|
|
|
|
116
|
my ($class, $x1, $y1, $x2, $y2) = @_; |
17
|
102
|
|
|
|
|
196
|
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
|
|
|
|
|
|
|
{ |
25
|
80
|
50
|
|
80
|
1
|
197
|
croak 'project not called with argument of type Math::Shape::Vector' unless $_[1]->isa('Math::Shape::Vector'); |
26
|
80
|
|
|
|
|
71
|
my ($self, $vector) = @_; |
27
|
80
|
|
|
|
|
130
|
my $unit_vector = $vector->convert_to_unit_vector; |
28
|
|
|
|
|
|
|
|
29
|
80
|
|
|
|
|
160
|
my $range = Math::Shape::Range->new( |
30
|
|
|
|
|
|
|
$unit_vector->dot_product($self->{start}), |
31
|
|
|
|
|
|
|
$unit_vector->dot_product($self->{end}), |
32
|
|
|
|
|
|
|
); |
33
|
|
|
|
|
|
|
|
34
|
80
|
|
|
|
|
152
|
$range->sort; |
35
|
|
|
|
|
|
|
} |
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
sub collides |
39
|
|
|
|
|
|
|
{ |
40
|
15
|
|
|
15
|
1
|
24
|
my ($self, $other_obj) = @_; |
41
|
|
|
|
|
|
|
|
42
|
15
|
100
|
|
|
|
153
|
if ($other_obj->isa('Math::Shape::LineSegment')) |
|
|
100
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
43
|
|
|
|
|
|
|
{ |
44
|
5
|
|
|
|
|
19
|
my $vector_a = $self->{end}->subtract_vector($self->{start}); |
45
|
5
|
|
|
|
|
29
|
my $axis_a = Math::Shape::Line->new( |
46
|
|
|
|
|
|
|
$self->{start}->{x}, |
47
|
|
|
|
|
|
|
$self->{start}->{y}, |
48
|
|
|
|
|
|
|
$vector_a->{x}, |
49
|
|
|
|
|
|
|
$vector_a->{y}); |
50
|
|
|
|
|
|
|
|
51
|
5
|
100
|
|
|
|
14
|
return 0 if $axis_a->on_one_side($other_obj); |
52
|
|
|
|
|
|
|
|
53
|
4
|
|
|
|
|
14
|
my $vector_b = $other_obj->{end}->subtract_vector($other_obj->{start}); |
54
|
4
|
|
|
|
|
20
|
my $axis_b = Math::Shape::Line->new( |
55
|
|
|
|
|
|
|
$other_obj->{start}->{x}, |
56
|
|
|
|
|
|
|
$other_obj->{start}->{y}, |
57
|
|
|
|
|
|
|
$vector_b->{x}, |
58
|
|
|
|
|
|
|
$vector_b->{y}); |
59
|
|
|
|
|
|
|
|
60
|
4
|
100
|
|
|
|
10
|
return 0 if $axis_b->on_one_side($self); |
61
|
|
|
|
|
|
|
|
62
|
3
|
100
|
|
|
|
12
|
if ($axis_a->{direction}->is_parallel($axis_b->{direction})) |
63
|
|
|
|
|
|
|
{ |
64
|
2
|
|
|
|
|
5
|
my $range_a = $self->project($axis_a->{direction}); |
65
|
2
|
|
|
|
|
5
|
my $range_b = $other_obj->project($axis_a->{direction}); |
66
|
2
|
|
|
|
|
7
|
return $range_a->is_overlapping($range_b); |
67
|
|
|
|
|
|
|
} |
68
|
|
|
|
|
|
|
|
69
|
1
|
|
|
|
|
18
|
1; |
70
|
|
|
|
|
|
|
} |
71
|
|
|
|
|
|
|
elsif ($other_obj->isa('Math::Shape::Vector')) |
72
|
|
|
|
|
|
|
{ |
73
|
2
|
|
|
|
|
6
|
$other_obj->collides($self); |
74
|
|
|
|
|
|
|
} |
75
|
|
|
|
|
|
|
elsif ($other_obj->isa('Math::Shape::Line')) |
76
|
|
|
|
|
|
|
{ |
77
|
2
|
|
|
|
|
5
|
$other_obj->collides($self); |
78
|
|
|
|
|
|
|
} |
79
|
|
|
|
|
|
|
elsif ($other_obj->isa('Math::Shape::OrientedRectangle')) |
80
|
|
|
|
|
|
|
{ |
81
|
2
|
|
|
|
|
8
|
$other_obj->collides($self); |
82
|
|
|
|
|
|
|
} |
83
|
|
|
|
|
|
|
elsif ($other_obj->isa('Math::Shape::Rectangle')) |
84
|
|
|
|
|
|
|
{ |
85
|
2
|
|
|
|
|
5
|
$other_obj->collides($self); |
86
|
|
|
|
|
|
|
} |
87
|
|
|
|
|
|
|
elsif ($other_obj->isa('Math::Shape::Circle')) |
88
|
|
|
|
|
|
|
{ |
89
|
2
|
|
|
|
|
7
|
$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
|
|
|
|
|
|
|
1; |
98
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
__END__ |