blib/lib/Geo/ShapeFile/Point.pm untested

filter: Statementstmt 0.0% 0 / 151 Branchbran 0.0% 0 / 52 Conditioncond 0.0% 0 / 39 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 47 Podpod 0.0% 0 / 41 totaltotal 0.0% 0 / 330 CC 27 SCAR 29.7
CC
27
Cov
0%
_mathemagic
90.0
eq
56.0
angle_to
12.0
CRAP 19.5
1 package Geo::ShapeFile::Point;
2 # TODO - add dimension operators (to specify if 2 or 3 dimensional point)
3 use strict;
4 use warnings;
5 use Math::Trig 1.04;
6 use Carp;
7  
8 our $VERSION = '3.02';
9  
10 use overload
11 '==' => 'eq',
12 'eq' => 'eq',
13 '""' => 'stringify',
14 '+' => \&add,
15 '-' => \&subtract,
16 '*' => \&multiply,
17 '/' => \&divide,
18 fallback => 1,
19 ;
20  
21 my %config = (
22 comp_includes_z => 1,
23 comp_includes_m => 1,
24 );
25  
26 sub new {
27 my $proto = shift;
28 my $class = ref($proto) || $proto;
29  
30 my $self = {@_};
31  
32 bless $self, $class;
33  
34 return $self;
35 }
36  
37 sub _var {
38 my $self = shift;
39 my $var = shift;
40  
41 if (@_) {
42 return $self->{$var} = shift;
43 }
44 else {
45 return $self->{$var};
46 }
47 }
48  
49 # these could be factory generated
50 sub X { shift()->_var('X', @_); }
51 sub Y { shift()->_var('Y', @_); }
52 sub Z { shift()->_var('Z', @_); }
53 sub M { shift()->_var('M', @_); }
54  
55 sub x_min { $_[0]->_var('X'); }
56 sub x_max { $_[0]->_var('X'); }
57 sub y_min { $_[0]->_var('Y'); }
58 sub y_max { $_[0]->_var('Y'); }
59 sub z_min { $_[0]->_var('Z'); }
60 sub z_max { $_[0]->_var('Z'); }
61 sub m_min { $_[0]->_var('M'); }
62 sub m_max { $_[0]->_var('M'); }
63  
64 sub get_x { $_[0]->{X} }
65 sub get_y { $_[0]->{Y} }
66 sub get_z { $_[0]->{Z} }
67 sub get_m { $_[0]->{M} }
68  
69  
70 sub import {
71 my $self = shift;
72 my %args = @_;
73  
74 foreach(keys %args) { $config{$_} = $args{$_}; }
75 }
76  
77 sub eq {
78 my $left = shift;
79 my $right = shift;
80  
81 if ($config{comp_includes_z} && (defined $left->Z || defined $right->Z)) {
82 return 0 unless defined $left->Z && defined $right->Z;
83 return 0 unless $left->Z == $right->Z;
84 }
85 if ($config{comp_includes_m} && (defined $left->M || defined $right->M)) {
86 return 0 unless defined $left->M && defined $right->M;
87 return 0 unless $left->M == $right->M;
88 }
89  
90 return ($left->X == $right->X && $left->Y == $right->Y);
91 }
92  
93 sub stringify {
94 my $self = shift;
95  
96 my @foo = ();
97 foreach(qw/X Y Z M/) {
98 if(defined $self->$_()) {
99 push @foo, "$_=" . $self->$_();
100 }
101 }
102 my $r = 'Point(' . join(',', @foo) . ')';
103 }
104  
105 sub distance_from {
106 my ($p1, $p2) = @_;
107  
108 my $dp = $p2->subtract($p1);
109 return sqrt ( ($dp->X ** 2) + ($dp->Y **2) );
110 }
111  
112 sub distance_to { distance_from(@_); }
113  
114 sub angle_to {
115 my ($p1, $p2) = @_;
116  
117 my $dp = $p2->subtract ($p1);
118  
119 my $x_off = $dp->get_x;
120 my $y_off = $dp->get_y;
121  
122 return 0 if !($x_off || $y_off);
123  
124 my $bearing = 90 - Math::Trig::rad2deg (Math::Trig::atan2 ($y_off, $x_off));
125 if ($bearing < 0) {
126 $bearing += 360;
127 }
128  
129 return $bearing;
130 }
131  
132 sub add { _mathemagic('add', @_); }
133 sub subtract { _mathemagic('subtract', @_); }
134 sub multiply { _mathemagic('multiply', @_); }
135 sub divide { _mathemagic('divide', @_); }
136  
137 sub _mathemagic {
138 my ($op, $l, $r, $reverse) = @_;
139  
140 if ($reverse) { # put them back in the right order
141 ($l, $r) = ($r, $l);
142 }
143 my ($left, $right);
144  
145 if (UNIVERSAL::isa($l, 'Geo::ShapeFile::Point')) { $left = 'point'; }
146 if (UNIVERSAL::isa($r, 'Geo::ShapeFile::Point')) { $right = 'point'; }
147  
148 if ($l =~ /^[\d\.]+$/) { $left = 'number'; }
149 if ($r =~ /^[\d\.]+$/) { $right = 'number'; }
150  
151 unless ($left) { croak "Couldn't identify $l for $op"; }
152 unless ($right) { croak "Couldn't identify $r for $op"; }
153  
154 my $function = '_' . join '_', $op, $left, $right;
155  
156 croak "Don't know how to $op $left and $right"
157 if !defined &{$function};
158  
159 do {
160 no strict 'refs';
161 return $function->($l, $r);
162 }
163 }
164  
165 sub _add_point_point {
166 my ($p1, $p2) = @_;
167  
168 my $z;
169 if(defined($p2->Z) && defined($p1->Z)) { $z = ($p2->Z + $p1->Z); }
170  
171 Geo::ShapeFile::Point->new(
172 X => ($p2->X + $p1->X),
173 Y => ($p2->Y + $p1->Y),
174 Z => $z,
175 );
176 }
177  
178 sub _add_point_number {
179 my ($p1, $n) = @_;
180  
181 my $z;
182 if (defined($p1->Z)) { $z = ($p1->Z + $n); }
183  
184 Geo::ShapeFile::Point->new(
185 X => ($p1->X + $n),
186 Y => ($p1->Y + $n),
187 Z => $z,
188 );
189 }
190 sub _add_number_point { add_point_number(@_); }
191  
192 sub _subtract_point_point {
193 my($p1, $p2) = @_;
194  
195 my $z;
196 if(defined($p2->Z) && defined($p1->Z)) { $z = ($p2->Z - $p1->Z); }
197  
198 my $result = Geo::ShapeFile::Point->new(
199 X => ($p1->X - $p2->X),
200 Y => ($p1->Y - $p2->Y),
201 Z => $z,
202 );
203 return $result;
204 }
205  
206 sub _subtract_point_number {
207 my($p1, $n) = @_;
208  
209 my $z;
210 if (defined $p1->Z) {
211 $z = ($p1->Z - $n);
212 }
213  
214 Geo::ShapeFile::Point->new(
215 X => ($p1->X - $n),
216 Y => ($p1->Y - $n),
217 Z => $z,
218 );
219 }
220 sub _subtract_number_point { _subtract_point_number(reverse @_); }
221  
222 sub _multiply_point_point {
223 my ($p1, $p2) = @_;
224  
225 my $z;
226 if (defined $p2->Z and defined $p1->Z) {
227 $z = $p2->Z * $p1->Z;
228 }
229  
230 Geo::ShapeFile::Point->new(
231 X => ($p2->X * $p1->X),
232 Y => ($p2->Y * $p1->Y),
233 Z => $z,
234 );
235 }
236 sub _multiply_point_number {
237 my($p1, $n) = @_;
238  
239 my $z;
240 if (defined $p1->Z) {
241 $z = $p1->Z * $n;
242 }
243  
244 Geo::ShapeFile::Point->new(
245 X => ($p1->X * $n),
246 Y => ($p1->Y * $n),
247 Z => $z,
248 );
249 }
250  
251 sub _multiply_number_point { _multiply_point_number(reverse @_); }
252  
253 sub _divide_point_point {
254 my($p1, $p2) = @_;
255  
256 my $z;
257 if (defined $p2->Z and defined $p1->Z) {
258 $z = $p1->Z / $p2->Z;
259 }
260  
261 Geo::ShapeFile::Point->new(
262 X => ($p1->X / $p2->X),
263 Y => ($p1->Y / $p2->Y),
264 Z => $z,
265 );
266 }
267  
268 sub _divide_point_number {
269 my ($p1, $n) = @_;
270  
271 my $z;
272 if (defined $p1->Z) {
273 $z = $p1->Z / $n;
274 }
275  
276 Geo::ShapeFile::Point->new(
277 X => ($p1->X / $n),
278 Y => ($p1->Y / $n),
279 Z => $z,
280 );
281 }
282  
283 sub _divide_number_point { divide_point_number(reverse @_); }
284  
285 1;
286 __END__