PlanePath/TriangleSpiralSkewed.pm untested

filter: Statementstmt 0.0% 0 / 144 Branchbran 0.0% 0 / 24 Conditioncond 0.0% 0 / 14 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 25 Podpod 0.0% 0 / 13 totaltotal 0.0% 0 / 220 CC 13 SCAR 29.4
CC
13
Cov
0%
n_to_xy
56.0
xy_to_n
42.0
new
6.0
CRAP 18.9
1 # Copyright 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020 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::TriangleSpiralSkewed;
20 use 5.004;
21 use strict;
22 #use List::Util 'max','min';
23 *max = \&Math::PlanePath::_max;
24 *min = \&Math::PlanePath::_min;
25  
26 use vars '$VERSION', '@ISA';
27 $VERSION = 128;
28 use Math::PlanePath;
29 @ISA = ('Math::PlanePath');
30  
31 use Math::PlanePath::Base::Generic
32 'round_nearest';
33 *_sqrtint = \&Math::PlanePath::_sqrtint;
34  
35 # uncomment this to run the ### lines
36 # use Smart::Comments;
37  
38 use constant xy_is_visited => 1;
39 use constant parameter_info_array =>
40 [
41 { name => 'skew',
42 type => 'enum',
43 share_key => 'skew_lrud',
44 display => 'Skew',
45 default => 'left',
46 choices => ['left', 'right','up','down' ],
47 choices_display => ['Left', 'Right','Up','Down' ],
48 },
49 Math::PlanePath::Base::Generic::parameter_info_nstart1(),
50 ];
51  
52 {
53 my %x_negative_at_n = (left => 3,
54 right => 5,
55 up => 3,
56 down => 5);
57 sub x_negative_at_n {
58 my ($self) = @_;
59 return $self->n_start + $x_negative_at_n{$self->{'skew'}};
60 }
61 }
62 {
63 my %y_negative_at_n = (left => 6,
64 right => 6,
65 up => 5,
66 down => 1);
67 sub y_negative_at_n {
68 my ($self) = @_;
69 return $self->n_start + $y_negative_at_n{$self->{'skew'}};
70 }
71 }
72 use constant dx_minimum => -1;
73 use constant dx_maximum => 1;
74 use constant dy_minimum => -1;
75 use constant dy_maximum => 1;
76 {
77 my %_UNDOCUMENTED__dxdy_list = (left => [1,0, # E
78 -1,1, # NW
79 0,-1], # S
80 right => [1,0, # E
81 0,1, # N
82 -1,-1], # SW
83 up => [1,1, # NE
84 -1,0, # W
85 0,-1], # S
86 down => [0,1, # N
87 -1,0, # W
88 1,-1], # SE
89 );
90 sub _UNDOCUMENTED__dxdy_list {
91 my ($self) = @_;
92 return @{$_UNDOCUMENTED__dxdy_list{$self->{'skew'}}};
93 }
94 }
95 {
96 my %dsumxy_minimum = (left => -1, # diagonal only NW across
97 right => -2, # SW
98 up => -1, # S
99 down => -1); # W
100 sub dsumxy_minimum {
101 my ($self) = @_;
102 return $dsumxy_minimum{$self->{'skew'}};
103 }
104 }
105 {
106 my %dsumxy_maximum = (left => 1, # E
107 right => 1, # N
108 up => 2, # NE
109 down => 1); # N
110 sub dsumxy_maximum {
111 my ($self) = @_;
112 return $dsumxy_maximum{$self->{'skew'}};
113 }
114 }
115 {
116 my %ddiffxy_minimum = (left => -2, # North-West
117 right => -1, # N
118 up => -1, # W
119 down => -1); # W
120 sub ddiffxy_minimum {
121 my ($self) = @_;
122 return $ddiffxy_minimum{$self->{'skew'}};
123 }
124 }
125 {
126 my %ddiffxy_maximum = (left => 1, # S
127 right => 1, # S
128 up => 1, # S
129 down => 2); # South-East
130 sub ddiffxy_maximum {
131 my ($self) = @_;
132 return $ddiffxy_maximum{$self->{'skew'}};
133 }
134 }
135  
136 {
137 my %dir_minimum_dxdy = (left => [1,0], # East
138 right => [1,0], # East
139 up => [1,1], # NE
140 down => [0,1]); # North
141 sub dir_minimum_dxdy {
142 my ($self) = @_;
143 return @{$dir_minimum_dxdy{$self->{'skew'}}};
144 }
145 }
146 {
147 my %dir_maximum_dxdy = (left => [0,-1], # South
148 right => [-1,-1], # South-West
149 up => [0,-1], # South
150 down => [1,-1]); # South-East
151 sub dir_maximum_dxdy {
152 my ($self) = @_;
153 return @{$dir_maximum_dxdy{$self->{'skew'}}};
154 }
155 }
156  
157 use constant turn_any_right => 0; # only left or straight
158  
159  
160 #------------------------------------------------------------------------------
161  
162 sub new {
163 my $self = shift->SUPER::new (@_);
164 if (! defined $self->{'n_start'}) {
165 $self->{'n_start'} = $self->default_n_start;
166 }
167 $self->{'skew'} ||= 'left';
168 return $self;
169 }
170  
171 # base at bottom left corner, N=0 basis, first loop d=1
172 # d = [ 1, 2, 3 ]
173 # n = [ 0, 6, 21 ]
174 # d = 5/6 + sqrt(2/9 * $n + 1/36)
175 # = (5 + sqrt(8N + 1))/6
176 # N = (9/2 d^2 - 15/2 d + 3)
177 # = (9/2*$d**2 - 15/2*$d + 3)
178 # = ((9/2*$d - 15/2)*$d + 3)
179 # = (9*$d - 15)*$d/2 + 3
180 #
181 # bottom right corner is further 3*$d along, so
182 # rem = $n - (9/2 d^2 - 15/2 d + 3) - 3*d
183 # = $n - (9/2 d^2 - 9/2 d + 3)
184 # = $n - (9/2*$d + -9/2)*$d - 3
185 # = $n - (9*$d + -9)*$d/2 - 3
186 # = $n - ($d - 1)*$d*9/2 - 3
187 # is rem < 0 bottom horizontal
188 # rem <= 3*d-1 right slope
189 # rem >= 3*d-1 left vertical
190 #
191 sub n_to_xy {
192 my ($self, $n) = @_;
193 #### TriangleSpiralSkewed n_to_xy: $n
194  
195 $n = $n - $self->{'n_start'}; # starting N==0, and warning if $n==undef
196 if ($n < 0) { return; }
197  
198 my $d = int((_sqrtint(8*$n + 1) + 5) / 6); # first loop d=1 at n=0
199 #### $d
200  
201 $n -= ($d-1)*$d/2 * 9;
202 #### remainder: $n
203  
204 my $zero = $n*0; # inherit BigFloat frac rather than $d=BigInt
205 my ($x,$y);
206  
207 if ($n <= 1) {
208 ### bottom horizontal: "nrem=$n"
209 $d -= 1;
210 $y = $zero - $d;
211 $x = $n + 2*$d;
212 } elsif (($n -= 3*$d) <= 0) {
213 ### right slope: "nrem=$n"
214 $x = -$n - $d;
215 $y = $n + 2*$d;
216 } else {
217 ### left vertical: "nrem=$n"
218 $x = $zero - $d;
219 $y = - $n + 2*$d;
220 }
221 ### xy skew=left: "$x,$y"
222  
223 if ($self->{'skew'} eq 'right') {
224 $x += $y;
225 } elsif ($self->{'skew'} eq 'up') {
226 $y += $x;
227 } elsif ($self->{'skew'} eq 'down') {
228 ($x,$y) = ($x+$y, -$x);
229 }
230 return ($x,$y);
231 }
232  
233 sub xy_to_n {
234 my ($self, $x, $y) = @_;
235 $x = round_nearest ($x);
236 $y = round_nearest ($y);
237 ### xy_to_n(): "$x,$y"
238  
239 if ($self->{'skew'} eq 'right') {
240 $x -= $y;
241 } elsif ($self->{'skew'} eq 'up') {
242 $y -= $x;
243 } elsif ($self->{'skew'} eq 'down') {
244 ($x,$y) = (-$y, $x+$y);
245 }
246 # now $x,$y in skew="left" style
247  
248 my $n;
249 if ($y < 0 && $y <= $x && $x <= -2*$y) {
250 ### bottom horizontal ...
251  
252 # negative y, vertical at x=0
253 # [ -1, -2, -3, -4 ]
254 # [ 8, 24, 49, 83 ]
255 # n = (9/2*$d**2 + -5/2*$d + 1)
256 #
257 $n = (9*$y - 5)*$y/2 + $x;
258  
259 } elsif ($x < 0 && $x <= $y && $y <= -2*$x) {
260 ### upper left vertical ...
261  
262 # negative x, horizontal at y=0
263 # [ -1, -2, -3, -4 ]
264 # [ 6, 20, 43, 75 ]
265 # n = (9/2*$d**2 + -1/2*$d + 1)
266 #
267 $n = (9*$x - 1)*$x/2 - $y;
268  
269 } else {
270 my $d = $x + $y;
271 ### upper right slope ...
272 ### $d
273  
274 # positive y, vertical at x=0
275 # [ 1, 2, 3, 4 ]
276 # [ 3, 14, 34, 63 ]
277 # n = (9/2*$d**2 + -5/2*$d + 1)
278 #
279 $n = (9*$d - 5)*$d/2 - $x;
280 }
281  
282 return $n + $self->{'n_start'};
283 }
284  
285 # n_hi exact, n_lo not
286 # not exact
287 sub rect_to_n_range {
288 my ($self, $x1,$y1, $x2,$y2) = @_;
289  
290 $x1 = round_nearest ($x1);
291 $y1 = round_nearest ($y1);
292 $x2 = round_nearest ($x2);
293 $y2 = round_nearest ($y2);
294  
295 return ($self->{'n_start'},
296 max ($self->xy_to_n ($x1,$y1),
297 $self->xy_to_n ($x1,$y2),
298 $self->xy_to_n ($x2,$y1),
299 $self->xy_to_n ($x2,$y2)));
300 }
301 # my $d = 0;
302 # foreach my $x ($x1, $x2) {
303 # foreach my $y ($y1, $y2) {
304 # $d = max ($d,
305 # 1 + ($y < 0 && $y <= $x && $x <= -2*$y
306 # ? -$y # bottom horizontal
307 # : $x < 0 && $x <= $y && $y <= 2*-$x
308 # ? -$x # left vertical
309 # : abs($x) + $y)); # right slope
310 # }
311 # }
312 # (9*$d - 9)*$d + 1 + $self->{'n_start'});
313  
314 1;
315 __END__