PlanePath/PyramidSpiral.pm untested

filter: Statementstmt 0.0% 0 / 92 Branchbran 0.0% 0 / 8 Conditioncond 0.0% 0 / 3 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 26 Podpod 0.0% 0 / 7 totaltotal 0.0% 0 / 136 CC 5 SCAR 12.6
CC
5
Cov
0%
n_to_xy
12.0
new
6.0
xy_to_n
6.0
CRAP 3.5
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::PyramidSpiral;
20 use 5.004;
21 use strict;
22 #use List::Util 'min';
23 *max = \&Math::PlanePath::_max;
24  
25 use vars '$VERSION', '@ISA';
26 $VERSION = 128;
27 use Math::PlanePath;
28 *_sqrtint = \&Math::PlanePath::_sqrtint;
29 @ISA = ('Math::PlanePath');
30  
31 use Math::PlanePath::Base::Generic
32 'round_nearest';
33  
34 # uncomment this to run the ### lines
35 #use Smart::Comments;
36  
37  
38 use constant parameter_info_array =>
39 [ Math::PlanePath::Base::Generic::parameter_info_nstart1() ];
40  
41 use constant xy_is_visited => 1;
42 sub x_negative_at_n {
43 my ($self) = @_;
44 return $self->n_start + 3;
45 }
46 sub y_negative_at_n {
47 my ($self) = @_;
48 return $self->n_start + 4;
49 }
50 sub _UNDOCUMENTED__dxdy_list_at_n {
51 my ($self) = @_;
52 return $self->n_start + 2;
53 }
54 use constant dx_minimum => -1;
55 use constant dx_maximum => 1;
56 use constant dy_minimum => -1;
57 use constant dy_maximum => 1;
58 use constant 1.02 _UNDOCUMENTED__dxdy_list => (1,0, # E
59 -1,1, # NW
60 -1,-1, # SW
61 );
62 use constant absdx_minimum => 1;
63 use constant dsumxy_minimum => -2; # SW diagonal
64 use constant dsumxy_maximum => 1;
65 use constant ddiffxy_minimum => -2; # NW diagonal
66 use constant ddiffxy_maximum => 1;
67 use constant dir_maximum_dxdy => (-1,-1); # South-West
68 use constant turn_any_right => 0; # only left or straight
69  
70  
71 #------------------------------------------------------------------------------
72  
73 sub new {
74 my $self = shift->SUPER::new (@_);
75 if (! defined $self->{'n_start'}) {
76 $self->{'n_start'} = $self->default_n_start;
77 }
78 return $self;
79 }
80  
81 # bottom left corner, N=0 basis
82 # [ 0, 1, 2, 3 ],
83 # [ 0, 4, 16, 36 ]
84 # N = (4 d^2)
85 # = (4*$d**2)
86 # = (4*$d**2)
87 # d = 0 + sqrt(1/4 * $n + 0)
88 # = sqrt($n)/2
89 #
90 sub n_to_xy {
91 my ($self, $n) = @_;
92 #### PyramidSpiral n_to_xy: $n
93  
94 $n = $n - $self->{'n_start'}; # start N=0, and warn if $n==undef
95 if ($n < 0) { return; }
96  
97 my $d = int( _sqrtint($n)/2 );
98 $n -= (4*$d+2)*$d; # to $n==0 on Y negative axis
99  
100 if ($n <= 2*$d+1) {
101 ### bottom horizontal ...
102 return ($n, -$d);
103 }
104 $n -= 4*$d+2;
105 ### sides, remainder pos/neg from top ...
106 return (-$n,
107 - abs($n) + $d + 1);
108 }
109  
110 # negative y, x=0 centres
111 # [ 1, 2, 3 ]
112 # [ 7, 21, 43 ]
113 # n = (4*$y*$y + 2*abs($y) + 1)
114 #
115 # positive y, x=0 centres
116 # [ 1, 2, 3 ]
117 # [ 3, 13, 31 ]
118 # n = (4*$d*$d + -2*$d + 1)
119 #
120  
121 sub xy_to_n {
122 my ($self, $x, $y) = @_;
123 $x = round_nearest ($x);
124 $y = round_nearest ($y);
125 ### xy_to_n(): "$x,$y"
126  
127 if ($y < 0 && abs($x) <= 2*-$y) {
128 ### bottom horizontal
129 return 4*$y*$y - 2*$y + $x + $self->{'n_start'};
130 }
131  
132 ### sides diagonal
133 my $k = 2 * (abs($x) + $y);
134 return $k*($k-1) - $x + $self->{'n_start'};
135 }
136  
137 # Each row N increases away from some midpoint.
138 # Each column N increase away from some midpoint.
139 # So maximum must be at one of the corners.
140 #
141 # maybe:
142 # Minimum in row is at X=0 when Y>=0
143 # or at X=2*Y slope when Y<0
144 # Minimum in column is at Y=floor(X/2) when X<=0
145 # or at Y=-floor(X/2) when X>=0
146 #
147 # not exact
148 sub rect_to_n_range {
149 my ($self, $x1,$y1, $x2,$y2) = @_;
150  
151 # ENHANCE-ME: find exact minimum
152 return ($self->n_start,
153 max ($self->xy_to_n($x1,$y1),
154 $self->xy_to_n($x1,$y2),
155 $self->xy_to_n($x2,$y1),
156 $self->xy_to_n($x2,$y2)));
157 }
158  
159 1;
160 __END__