line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Geometry::Primitive::Line; |
2
|
1
|
|
|
1
|
|
10111
|
use Moose; |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
use MooseX::Storage; |
4
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
with qw(Geometry::Primitive::Shape MooseX::Clone MooseX::Storage::Deferred); |
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
use overload ('""' => 'to_string'); |
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
use Geometry::Primitive::Point; |
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
has 'start' => ( |
12
|
|
|
|
|
|
|
is => 'rw', |
13
|
|
|
|
|
|
|
isa => 'Geometry::Primitive::Point', |
14
|
|
|
|
|
|
|
required => 1, |
15
|
|
|
|
|
|
|
coerce => 1 |
16
|
|
|
|
|
|
|
); |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
has 'end' => ( |
19
|
|
|
|
|
|
|
is => 'rw', |
20
|
|
|
|
|
|
|
isa => 'Geometry::Primitive::Point', |
21
|
|
|
|
|
|
|
required => 1, |
22
|
|
|
|
|
|
|
coerce => 1 |
23
|
|
|
|
|
|
|
); |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
sub contains_point { |
26
|
|
|
|
|
|
|
my ($self, $x, $y) = @_; |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
my $point; |
29
|
|
|
|
|
|
|
if(!ref($x) && defined($y)) { |
30
|
|
|
|
|
|
|
# This allows the user to pass in $x and $y as scalars, which |
31
|
|
|
|
|
|
|
# easier sometimes. |
32
|
|
|
|
|
|
|
$point = Geometry::Primitive::Point->new(x => $x, y => $y); |
33
|
|
|
|
|
|
|
} else { |
34
|
|
|
|
|
|
|
$point = $x; |
35
|
|
|
|
|
|
|
} |
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
my $expy = ($self->slope * $point->x) + $self->y_intercept; |
38
|
|
|
|
|
|
|
return $expy == $point->y; |
39
|
|
|
|
|
|
|
} |
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
# Don't know how to do this atm. |
42
|
|
|
|
|
|
|
sub scale { } |
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
sub is_parallel { |
45
|
|
|
|
|
|
|
my ($self, $line) = @_; |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
return $line->slope == $self->slope; |
48
|
|
|
|
|
|
|
} |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
sub is_perpendicular { |
51
|
|
|
|
|
|
|
my ($self, $line) = @_; |
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
my $slope = $self->slope; |
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
# Deal with horizontal and vertical lines |
56
|
|
|
|
|
|
|
if(!defined($slope)) { |
57
|
|
|
|
|
|
|
return $line->slope == 0; |
58
|
|
|
|
|
|
|
} |
59
|
|
|
|
|
|
|
if($slope == 0) { |
60
|
|
|
|
|
|
|
return !defined($line->slope); |
61
|
|
|
|
|
|
|
} |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
return $line->slope == (-1 / $self->slope); |
64
|
|
|
|
|
|
|
} |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
sub length { |
67
|
|
|
|
|
|
|
my ($self) = @_; |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
return sqrt(($self->end->x - $self->start->x) ** 2 |
70
|
|
|
|
|
|
|
+ ($self->end->y - $self->start->y) ** 2); |
71
|
|
|
|
|
|
|
} |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
sub point_end { |
74
|
|
|
|
|
|
|
my ($self) = @_; return $self->end; |
75
|
|
|
|
|
|
|
} |
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
sub point_start { |
78
|
|
|
|
|
|
|
my ($self) = @_; return $self->start; |
79
|
|
|
|
|
|
|
} |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
sub slope { |
82
|
|
|
|
|
|
|
my ($self) = @_; |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
my $end = $self->end; |
85
|
|
|
|
|
|
|
my $start = $self->start; |
86
|
|
|
|
|
|
|
my $x = $end->x - $start->x; |
87
|
|
|
|
|
|
|
my $y = $end->y - $start->y; |
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
if($x == 0) { |
90
|
|
|
|
|
|
|
return undef; |
91
|
|
|
|
|
|
|
} |
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
return $y / $x; |
94
|
|
|
|
|
|
|
} |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
sub to_string { |
97
|
|
|
|
|
|
|
my ($self) = @_; |
98
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
return $self->start->to_string." - ".$self->end->to_string; |
100
|
|
|
|
|
|
|
} |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
sub y_intercept { |
103
|
|
|
|
|
|
|
my ($self) = @_; |
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
return $self->start->y - ($self->slope * $self->start->x); |
106
|
|
|
|
|
|
|
} |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
__PACKAGE__->meta->make_immutable; |
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
no Moose; |
111
|
|
|
|
|
|
|
1; |
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
__END__ |
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
=head1 NAME |
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
Geometry::Primitive::Line - A Line |
118
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
=head1 DESCRIPTION |
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
Geometry::Primitive::Line represents a straight curve defined by two points. |
122
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
=head1 SYNOPSIS |
124
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
use Geometry::Primitive::Line; |
126
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
my $line = Geometry::Primitive::Line->new(); |
128
|
|
|
|
|
|
|
$line->start($point1); |
129
|
|
|
|
|
|
|
$line->end($point2); |
130
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
=head1 ATTRIBUTES |
132
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
=head2 end |
134
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
Set/Get the end point of the line. |
136
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
=head2 start |
138
|
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
Set/Get the start point of the line. |
140
|
|
|
|
|
|
|
|
141
|
|
|
|
|
|
|
=head1 METHODS |
142
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
=head2 new |
144
|
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
Creates a new Geometry::Primitive::Line |
146
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
=head2 contains_point |
148
|
|
|
|
|
|
|
|
149
|
|
|
|
|
|
|
Returns true if the supplied point is 'on' the line. Accepts either a point |
150
|
|
|
|
|
|
|
object or an x y pair. |
151
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
=head2 grow |
153
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
Does nothing, as I'm not sure how. Patches or hints welcome. |
155
|
|
|
|
|
|
|
|
156
|
|
|
|
|
|
|
=head2 is_parallel ($other_line) |
157
|
|
|
|
|
|
|
|
158
|
|
|
|
|
|
|
Returns true if the supplied line is parallel to this one. |
159
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
=head2 is_perpendicular ($other_line) |
161
|
|
|
|
|
|
|
|
162
|
|
|
|
|
|
|
Returns true if the supplied line is perpendicular to this one. |
163
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
=head2 length |
165
|
|
|
|
|
|
|
|
166
|
|
|
|
|
|
|
Get the length of the line. |
167
|
|
|
|
|
|
|
|
168
|
|
|
|
|
|
|
=head2 point_end |
169
|
|
|
|
|
|
|
|
170
|
|
|
|
|
|
|
Get the end point. Provided for Shape role. |
171
|
|
|
|
|
|
|
|
172
|
|
|
|
|
|
|
=head2 point_start |
173
|
|
|
|
|
|
|
|
174
|
|
|
|
|
|
|
Get the start point. Provided for Shape role. |
175
|
|
|
|
|
|
|
|
176
|
|
|
|
|
|
|
=head2 scale |
177
|
|
|
|
|
|
|
|
178
|
|
|
|
|
|
|
Does nothing at the moment. |
179
|
|
|
|
|
|
|
|
180
|
|
|
|
|
|
|
=head2 slope |
181
|
|
|
|
|
|
|
|
182
|
|
|
|
|
|
|
Get the slope of the line. |
183
|
|
|
|
|
|
|
|
184
|
|
|
|
|
|
|
=head2 to_string |
185
|
|
|
|
|
|
|
|
186
|
|
|
|
|
|
|
Guess! |
187
|
|
|
|
|
|
|
|
188
|
|
|
|
|
|
|
=head2 y_intercept |
189
|
|
|
|
|
|
|
|
190
|
|
|
|
|
|
|
Returns the Y intercept of this line. |
191
|
|
|
|
|
|
|
|
192
|
|
|
|
|
|
|
=head1 AUTHOR |
193
|
|
|
|
|
|
|
|
194
|
|
|
|
|
|
|
Cory Watson <gphat@cpan.org> |
195
|
|
|
|
|
|
|
|
196
|
|
|
|
|
|
|
=head1 COPYRIGHT & LICENSE |
197
|
|
|
|
|
|
|
|
198
|
|
|
|
|
|
|
You can redistribute and/or modify this code under the same terms as Perl |
199
|
|
|
|
|
|
|
itself. |