PlanePath/PowerArray.pm untested

filter: Statementstmt 0.0% 0 / 87 Branchbran 0.0% 0 / 26 Conditioncond 0.0% 0 / 11 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 18 Podpod 0.0% 0 / 9 totaltotal 0.0% 0 / 151 CC 14 SCAR 25.9
CC
14
Cov
0%
rect_to_n_range
42.0
n_to_xy
20.0
absdx_minimum
6.0
CRAP 13.4
1 # Copyright 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019 Kevin Ryde
2  
3 # This file is part of Math-PlanePath.
4 #
5 # Math-PlanePath is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by the
7 # Free Software Foundation; either version 3, or (at your option) any later
8 # version.
9 #
10 # Math-PlanePath is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 # for more details.
14 #
15 # You should have received a copy of the GNU General Public License along
16 # with Math-PlanePath. If not, see <http://www.gnu.org/licenses/>.
17  
18 # area
19  
20 package Math::PlanePath::PowerArray;
21 use 5.004;
22 use strict;
23 use List::Util 'max';
24  
25 use vars '$VERSION', '@ISA';
26 $VERSION = 128;
27 use Math::PlanePath;
28 @ISA = ('Math::PlanePath');
29  
30 use Math::PlanePath::Base::Generic
31 'is_infinite',
32 'round_nearest';
33 use Math::PlanePath::Base::Digits
34 'parameter_info_array';
35  
36 # uncomment this to run the ### lines
37 #use Smart::Comments;
38  
39 use constant class_x_negative => 0;
40 use constant class_y_negative => 0;
41 *xy_is_visited = \&Math::PlanePath::Base::Generic::xy_is_visited_quad1;
42  
43 sub absdx_minimum {
44 my ($self) = @_;
45 return ($self->{'radix'} == 2
46 ? 1
47 : 0); # at N=1 dX=0,dY=1
48 }
49 sub absdy_minimum {
50 my ($self) = @_;
51 return ($self->{'radix'} == 2
52 ? 0 # at N=1 dX=1,dY=0
53 : 1); # always different Y
54 }
55  
56 sub dir_minimum_dxdy {
57 my ($self) = @_;
58 return ($self->{'radix'} == 2
59 ? (1,0) # East
60 : (0,1)); # North
61 }
62 sub dir_maximum_dxdy {
63 my ($self) = @_;
64 my $radix = $self->{'radix'};
65 return $self->n_to_dxdy($radix==2 ? 3 : $radix-1);
66 }
67  
68 sub turn_any_straight {
69 my ($self) = @_;
70 return ($self->{'radix'} != 3); # radix=3 never straight
71 }
72  
73  
74 #------------------------------------------------------------------------------
75 sub new {
76 my $self = shift->SUPER::new (@_);
77 $self->{'radix'} = max ($self->{'radix'} || 0, 2); # default 2
78 return $self;
79 }
80  
81 sub n_to_xy {
82 my ($self, $n) = @_;
83 ### PowerArray n_to_xy(): $n
84  
85 if ($n < 1) { return; }
86 if (is_infinite($n) || $n == 0) { return ($n,$n); }
87  
88 {
89 # fractions on straight line ?
90 my $int = int($n);
91 if ($n != $int) {
92 my $frac = $n - $int; # inherit possible BigFloat/BigRat
93 my ($x1,$y1) = $self->n_to_xy($int);
94 my ($x2,$y2) = $self->n_to_xy($int+1);
95 my $dx = $x2-$x1;
96 my $dy = $y2-$y1;
97 return ($frac*$dx + $x1, $frac*$dy + $y1);
98 }
99 $n = $int;
100 }
101  
102 my $x = $n*0;
103 my $radix = $self->{'radix'};
104 until ($n % $radix) {
105 $x++;
106 $n /= $radix;
107 }
108 ### $x
109 ### $n
110  
111 return ($x,
112 $n - int($n/$radix) - 1); # collapse out multiples of radix
113 }
114  
115 # | 9
116 # | 8
117 # | 7
118 # 4 | 6 30
119 # 3 | 4 20
120 # 2 | 3 15
121 # 1 | 2 10
122 # 0 | 1 5 25 125
123 # +------------
124 #
125 sub xy_to_n {
126 my ($self, $x, $y) = @_;
127 ### PowerArray xy_to_n(): "$x, $y"
128  
129 $x = round_nearest ($x);
130 $y = round_nearest ($y);
131 if ($x < 0 || $y < 0) {
132 return undef;
133 }
134 my $radix = $self->{'radix'};
135 return ($radix + 0*$y) ** $x # $y*0 to inherit bignum in power
136 * ($y+1 + int($y/($radix-1))); # stretch multiples of radix
137 }
138  
139 # N=..004 X=0 Y=N-floor(N/5)
140 # N=..010 X=1 Y=N/5-floor(N/25) dX=1 dY=...
141 # N=..011 X=0 Y=(N-1)/5-floor((N-1)/25) dX=-1 dY=0
142 # sub n_to_dxdy {
143 # my ($self, $n) = @_;
144 # }
145  
146 # exact
147 sub rect_to_n_range {
148 my ($self, $x1,$y1, $x2,$y2) = @_;
149 ### PowerArray rect_to_n_range(): "$x1,$y1 $x2,$y2"
150  
151 $x1 = round_nearest ($x1);
152 $y1 = round_nearest ($y1);
153 $x2 = round_nearest ($x2);
154 $y2 = round_nearest ($y2);
155  
156 ($x1,$x2) = ($x2,$x1) if $x1 > $x2;
157 ($y1,$y2) = ($y2,$y1) if $y1 > $y2;
158  
159 if ($x2 < 0 || $y2 < 0) {
160 ### all outside first quadrant ...
161 return (1, 0);
162 }
163  
164 # bottom left into first quadrant
165 if ($x1 < 0) { $x1 *= 0; } # *=0 to preserve bigint
166 if ($y1 < 0) { $y1 *= 0; }
167  
168 return ($self->xy_to_n($x1,$y1), # bottom left
169 $self->xy_to_n($x2,$y2)); # top right
170 }
171  
172 1;
173 __END__