blib/lib/Math/Project.pm untested

filter: Statementstmt 0.0% 0 / 44 Branchbran 0.0% 0 / 22 Conditioncond 0.0% 0 / 18 MC/DCmcdc 0.0% 0 / 11 Subroutinesub 0.0% 0 / 7 Podpod 0.0% 0 / 5 totaltotal 0.0% 0 / 107 CC 12 SCAR 30.7
CC
12
Cov
0%
_project
42.0
abscissa_project
20.0
_sign
12.0
CRAP 21.4
1 package Math::Project 0.04;
2 # ABSTRACT: Compute intersection with upright line through input point
3 $Math::Project::VERSION = '0.04';
4 use strict;
5 use base 'Exporter';
6  
7 our @EXPORT_OK = qw/project abscissa_project/;
8  
9  
10 sub _sign {
11 my $x = shift;
12 return -1 if $x < 0;
13 return +1 if $x > 0;
14 return 0;
15 }
16  
17 sub _round {
18 my $f = shift;
19 return int ($f+0.5);
20 }
21  
22 sub _project {
23 my ($x1,$y1,$x2,$y2,$xi,$yi) = @_;
24  
25 return [ $x1,$y1,0 ] if $x1 == $xi and $y1 == $yi;
26 return [ $x2,$y2,0 ] if $x2 == $xi and $y2 == $yi;
27 return [ $x1,$y1,0 ] if $x1 == $x2 and $y1 == $y2;
28  
29 my $dx = $x2-$x1; my $dy = $y2-$y1;
30  
31 my $l = sqrt($dx*$dx+$dy*$dy);
32 my $b = sqrt(($xi-$x1)*($xi-$x1)+($yi-$y1)*($yi-$y1));
33 my $c = sqrt(($xi-$x2)*($xi-$x2)+($yi-$y2)*($yi-$y2));
34 my $a = ($b*$b-$c*$c+$l*$l)/(2*$l);
35 my $d = sqrt($b*$b-$a*$a);
36  
37 my $xo = ($a/$l) * $dx;
38 my $yo = ($a/$l) * $dy;
39  
40 my $abscissa = 0;
41 ++$abscissa if _sign($dx) == _sign($xo) and _sign($dy) == _sign($yo)
42 and (abs($xo) < abs($dx) or abs($yo) < abs($dy));
43  
44 my @res = (_round($x1+$xo), _round($y1+$yo), _round(abs($d)),
45 $abscissa);
46  
47 return wantarray ? @res : \@res;
48 }
49  
50 sub project {
51 my @res = _project(@_);
52 pop @res;
53 return wantarray ? @res : \@res;
54 }
55  
56 sub abscissa_project {
57 my @res = _project(@_);
58 my $a = pop @res;
59 return wantarray ? () : undef unless $a;
60 return wantarray ? @res : \@res;
61 }
62  
63 1;
64  
65 __END__