PlanePath/DiagonalsOctant.pm untested

filter: Statementstmt 0.0% 0 / 118 Branchbran 0.0% 0 / 50 Conditioncond 0.0% 0 / 17 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 26 Podpod 0.0% 0 / 13 totaltotal 0.0% 0 / 224 CC 26 SCAR 29.5
CC
26
Cov
0%
rect_to_n_range
72.0
n_to_xy
30.0
xy_to_n
20.0
CRAP 19.1
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  
19 package Math::PlanePath::DiagonalsOctant;
20 use 5.004;
21 use strict;
22 use Carp 'croak';
23  
24 use vars '$VERSION', '@ISA';
25 $VERSION = 128;
26 use Math::PlanePath;
27 *_sqrtint = \&Math::PlanePath::_sqrtint;
28 @ISA = ('Math::PlanePath');
29  
30 use Math::PlanePath::Base::Generic
31 'round_nearest';
32  
33 # uncomment this to run the ### lines
34 #use Smart::Comments;
35  
36  
37 use constant class_x_negative => 0;
38 use constant class_y_negative => 0;
39 use constant n_frac_discontinuity => .5;
40  
41 use constant parameter_info_array =>
42 [ { name => 'direction',
43 share_key => 'direction_downup',
44 display => 'Direction',
45 type => 'enum',
46 default => 'down',
47 choices => ['down','up'],
48 choices_display => ['Down','Up'],
49 description => 'Number points downwards or upwards along the diagonals.',
50 },
51 Math::PlanePath::Base::Generic::parameter_info_nstart1(),
52 ];
53  
54 use constant diffxy_maximum => 0; # octant X<=Y so X-Y<=0
55  
56 sub dx_minimum {
57 my ($self) = @_;
58 return ($self->{'direction'} eq 'up' ? -1 : undef);
59 }
60 sub dx_maximum {
61 my ($self) = @_;
62 return ($self->{'direction'} eq 'down' ? 1 : undef);
63 }
64  
65 sub dy_minimum {
66 my ($self) = @_;
67 return ($self->{'direction'} eq 'down' ? -1 : undef);
68 }
69 sub dy_maximum {
70 my ($self) = @_;
71 return ($self->{'direction'} eq 'up' ? 1 : undef);
72 }
73  
74 sub absdy_minimum {
75 my ($self) = @_;
76 return ($self->{'direction'} eq 'down'
77 ? 1 # 'down' always changes
78 : 0); # 'up' N=2 dY=0
79 }
80  
81 use constant dsumxy_minimum => 0; # advancing diagonals
82 use constant dsumxy_maximum => 1;
83 sub ddiffxy_minimum {
84 my ($self) = @_;
85 return ($self->{'direction'} eq 'down'
86 ? undef # "down" jumps back unlimited at bottom
87 : -2); # NW diagonal
88 }
89 sub ddiffxy_maximum {
90 my ($self) = @_;
91 return ($self->{'direction'} eq 'down'
92 ? 2 # SE diagonal
93 : undef); # "up" jumps down unlimited at top
94 }
95  
96 sub dir_minimum_dxdy {
97 my ($self) = @_;
98 return ($self->{'direction'} eq 'down'
99 ? (0,1) # vertical N=1to2
100 : (1,0)); # horizontal N=2to3
101 }
102 sub dir_maximum_dxdy {
103 my ($self) = @_;
104 return ($self->{'direction'} eq 'down'
105 ? (1,-1) # South-East diagonal
106 : (2,-1)); # N=6 to N=7
107 }
108  
109  
110 #------------------------------------------------------------------------------
111  
112 sub new {
113 my $self = shift->SUPER::new(@_);
114 if (! defined $self->{'n_start'}) {
115 $self->{'n_start'} = $self->default_n_start;
116 }
117  
118 my $direction = ($self->{'direction'} ||= 'down');
119 if (! ($direction eq 'up' || $direction eq 'down')) {
120 croak "Unrecognised direction option: ", $direction;
121 }
122  
123 return $self;
124 }
125  
126 # start from integers
127 # [ 1, 2, 3, 4, 5 ],
128 # [ 1, 3, 7, 13, 21 ]
129 # N = (d^2 - d + 1)
130 # = ($d**2 - $d + 1)
131 # = (($d - 1)*$d + 1)
132 #
133 # starting 0.5 back from the odd Y, numbering d=1 for the Y=1
134 # [ 1, 2, 3, 4, 5 ],
135 # [ 2-.5, 5-.5, 10-.5, 17-.5, 26-.5 ] # squares +0.5
136 # N = (d^2 + 1/2)
137 # = ($d**2 + 1/2)
138 # = ($d**2 + 1/2)
139 # d = 0 + sqrt(1 * $n + -1/2)
140 # = sqrt($n -1/2)
141 # = sqrt(4*$n-2)/2
142 #
143 # 9 | 26
144 # 8 | 21
145 # 7 | 17 22
146 # 6 | 13 18 23
147 # 5 | 10 14 19 24
148 # 4 | 7 11 15 20 25
149 # 3 | 5 8 12 16
150 # 2 | 3 6 9
151 # 1 | 2 4
152 # Y=0 | 1
153 # +-------------
154 #
155 sub n_to_xy {
156 my ($self, $n) = @_;
157 ### DiagonalsOctant n_to_xy(): "$n ".(ref $n || '')
158  
159 # adjust to N=1 at origin X=0,Y=0
160 $n = $n - $self->{'n_start'} + 1;
161  
162 my $d = int(4*$n)-2; # for sqrt
163 if ($d < 0) {
164 ### nothing at N < 0.5 ...
165 return;
166 }
167 $d = int( _sqrtint($d)/2 );
168 ### $d
169  
170 # remainder positive or negative relative to the start of the following
171 # diagonal
172 #
173 $n -= $d*($d+1) + 1;
174 ### remainder: $n
175  
176 # $n first in formulas to preserve n=BigFloat when d=integer is BigInt
177 #
178 if ($self->{'direction'} eq 'up') {
179 if (2*$n >= -1) {
180 return (-$n + $d,
181 $n + $d);
182 } else {
183 return (-$n - 1,
184 $n + 2*$d);
185 }
186 } else {
187 if (2*$n >= -1) {
188 # stripe of d+1 many points starting at Y even, eg. N=13
189 return ($n,
190 -$n + 2*$d);
191 } else {
192 # stripe of d many points starting at Y odd, eg. N=10
193 return ($n + $d,
194 -$n + $d - 1);
195 }
196 }
197 }
198  
199 sub xy_to_n {
200 my ($self, $x, $y) = @_;
201 ### xy_to_n(): $x, $y
202  
203 $x = round_nearest ($x);
204 if ($self->{'direction'} eq 'up') {
205 $y = round_nearest ($y);
206 } else {
207 $y = - round_nearest (- $y);
208 }
209  
210 ### rounded
211 ### $x
212 ### $y
213  
214 if ($y < 0 || $x < 0 || $x > $y) {
215 ### outside upper octant ...
216 return undef;
217 }
218  
219 if ($self->{'direction'} eq 'up') {
220 my $d = $x + $y + 2;
221 ### $d
222 return ($d*$d - ($d % 2))/4 - $x + $self->{'n_start'} - 1;
223 } else {
224 my $d = $x + $y + 1;
225 ### $d
226 return ($d*$d - ($d % 2))/4 + $x + $self->{'n_start'};
227 }
228 }
229  
230 # exact
231 sub rect_to_n_range {
232 my ($self, $x1,$y1, $x2,$y2) = @_;
233  
234 $x1 = round_nearest ($x1);
235 $x2 = round_nearest ($x2);
236 if ($self->{'direction'} eq 'up') {
237 $y1 = round_nearest ($y1);
238 $y2 = round_nearest ($y2);
239 } else {
240 $y1 = - round_nearest (- $y1);
241 $y2 = - round_nearest (- $y2);
242 }
243  
244 # bottom-left and top-right same as Math::PlanePath::Diagonals, but also
245 # brining $y1 up to within octant
246  
247 if ($x1 > $x2) { ($x1,$x2) = ($x2,$x1); }
248 if ($y1 > $y2) { ($y1,$y2) = ($y2,$y1); }
249  
250 # x2 | /
251 # -----+ | /
252 # | |/ +----
253 # -----+ + |x1,y2
254 #
255 if ($x2 < 0 || $y2 < 0 || $x1 > $y2) {
256 ### entirely outside upper octant, no range ...
257 return (1, 0);
258 }
259  
260 # |
261 # +---- /
262 # | | /
263 # +---- /
264 # x1 | /
265 # +
266 # increase x1 to within octant
267 if ($x1 < 0) { $x1 *= 0; } # zero by $x1*0 to preserve bignum
268  
269 # | | /
270 # | |/
271 # | /|
272 # | / +----y1
273 # + x1
274 # increase y1 so bottom-left x1,y1 is within octant
275 if ($y1 < $x1) { $y1 = $x1; }
276  
277 # | / x2
278 # | --------+
279 # | / |
280 # | -------+
281 # | /
282 # +
283 # decrease x2 so top-right is within octant
284 if ($x2 > $y2) { $x2 = $y2; }
285  
286 # exact range bottom left to top right
287 return ($self->xy_to_n ($x1,$y1),
288 $self->xy_to_n ($x2,$y2));
289 }
290  
291 1;
292 __END__