PlanePath/PentSpiral.pm untested

filter: Statementstmt 0.0% 0 / 112 Branchbran 0.0% 0 / 26 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 25 Podpod 0.0% 0 / 7 totaltotal 0.0% 0 / 170 CC 14 SCAR 26.5
CC
14
Cov
0%
xy_to_n
42.0
n_to_xy
30.0
rect_to_n_range
20.0
CRAP 14.1
1 # Copyright 2010, 2011, 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::PentSpiral;
20 use 5.004;
21 use strict;
22 #use List::Util 'max';
23 *max = \&Math::PlanePath::_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 'round_nearest';
32 *_sqrtint = \&Math::PlanePath::_sqrtint;
33  
34 # uncomment this to run the ### lines
35 #use Smart::Comments;
36  
37  
38 use constant parameter_info_array =>
39 [
40 Math::PlanePath::Base::Generic::parameter_info_nstart1(),
41 ];
42  
43 sub x_negative_at_n {
44 my ($self) = @_;
45 return $self->n_start + 3;
46 }
47 sub y_negative_at_n {
48 my ($self) = @_;
49 return $self->n_start + 4;
50 }
51 sub _UNDOCUMENTED__dxdy_list_at_n {
52 my ($self) = @_;
53 return $self->n_start + 6;
54 }
55  
56 use constant dx_minimum => -2;
57 use constant dx_maximum => 2;
58 use constant dy_minimum => -1;
59 use constant dy_maximum => 1;
60 use constant 1.02 _UNDOCUMENTED__dxdy_list => (2,0, # E by 2
61 1,1, # NE
62 -2,1, # WNW
63 -2,-1, # WSW
64 1,-1, # SE
65 );
66 use constant absdx_minimum => 1;
67 use constant dsumxy_minimum => -3; # SW -2,-1
68 use constant dsumxy_maximum => 2; # dX=+2 and NE diag
69 use constant ddiffxy_minimum => -3; # NW dX=-2,dY=+1
70 use constant ddiffxy_maximum => 2;
71 use constant dir_maximum_dxdy => (1,-1); # South-East
72 use constant turn_any_right => 0; # only left or straight
73  
74  
75 #------------------------------------------------------------------------------
76  
77 sub new {
78 my $self = shift->SUPER::new(@_);
79 if (! defined $self->{'n_start'}) {
80 $self->{'n_start'} = $self->default_n_start;
81 }
82 return $self;
83 }
84  
85 # base South-West diagonal
86 # d = [ 1, 2, 3, 4 ]
87 # n = [ 0, 4, 13, 27 ]
88 # N = (5/2 d^2 - 7/2 d + 1)
89 # = (5/2*$d**2 - 7/2*$d + 1)
90 # = ((5/2*$d - 7/2)*$d + 1)
91 # d = 7/10 + sqrt(2/5 * $n + 9/100)
92 # = (sqrt(40*$n + 9) + 7) / 10
93 #
94 # split Y axis
95 # d = [ 1, 2, 3 ]
96 # n = [ 2, 9, 21 ]
97 # N = ((5/2*$d - 1/2)*$d)
98  
99 sub n_to_xy {
100 my ($self, $n) = @_;
101 #### n_to_xy: $n
102  
103 # adjust to N=0 at origin X=0,Y=0
104 $n = $n - $self->{'n_start'};
105 if ($n < 0) { return; }
106  
107 my $d = int( (_sqrtint(40*$n+9)+7)/10 );
108 $n -= (5*$d-1)*$d/2;
109  
110 if ($n < -$d) {
111 $n += 2*$d;
112 if ($n < 1) {
113 # bottom horizontal
114 return (2*$n+$d-1, -$d+1);
115 } else {
116 # lower right diagonal ...
117 return ($n+$d, $n-$d);
118 }
119 } else {
120 if ($n <= $d) {
121 ### top 2,1 slope left and right diagonals ...
122 return (-2*$n,
123 -abs($n) + $d);
124 } else {
125 ### lower left diagonal ...
126 return ($n - 3*$d,
127 -$n + $d);
128 }
129 }
130 }
131  
132 sub xy_to_n {
133 my ($self, $x, $y) = @_;
134  
135 $x = round_nearest ($x);
136 $y = round_nearest ($y);
137  
138 # nothing on odd points
139 # when y>=0 any odd x is not covered
140 # when y<0 the uncovered alternates, x even on y=-1, x odd on y=-2, x even
141 # y=-3 etc
142 if (($x%2) ^ ($y < 0 ? $y%2 : 0)) {
143 return undef;
144 }
145  
146 if ($y >= 0) {
147 ### top left and right slopes
148 # vertical at x=0
149 # d = [ 1, 2, 3 ]
150 # n = [ 3, 10, 22 ]
151 # n = (5/2*$d**2 + -1/2*$d + 1)
152 #
153 ### assert: ($x%2)==0
154 $x /= 2;
155 my $d = abs($x) + $y;
156 return (5*$d - 1)*$d/2 - $x + $self->{'n_start'};
157 }
158  
159 if ($x < $y) {
160 ### lower left slope
161 # horizontal leftwards at y=0
162 # d = [ 1, 2, 3 ]
163 # n = [ 4, 12, 25 ]
164 # n = (5/2*$d**2 + 1/2*$d + 1)
165 # = (2.5*$d + 0.5)*$d + 1
166 my $d = -($x+$y)/2;
167 return (5*$d + 1)*$d/2 - $y + $self->{'n_start'};
168 }
169  
170 if ($x > -$y) {
171 ### lower right slope
172 # horizontal rightwards at y=0
173 # d = [ 1, 2, 3, ]
174 # n = [ 2, 8, 19,]
175 # n = (5/2*$d**2 + -3/2*$d + 1)
176 # = (2.5*$d - 1.5)*$d + 1
177 my $d = ($x-$y)/2;
178 return (5*$d - 3)*$d/2 + $y + $self->{'n_start'};
179 }
180  
181 ### bottom horizontal
182 # vertical downwards at x=0 is
183 # y = [ -1, -2, -3 ]
184 # n = [ 5.5, 15, 29.5 ]
185 # n = (5/2*$y**2 + -2*$y + 1)
186 # = (2.5*$y - 2)*$y + 1
187 # so
188 # N = (2.5*$y - 2)*$y + 1 + $x/2
189 # = ((5*$y - 4)*$y + $x)/2 + 1
190 #
191 return ((5*$y-4)*$y + $x)/2 + $self->{'n_start'};
192 }
193  
194 # not exact
195 sub rect_to_n_range {
196 my ($self, $x1,$y1, $x2,$y2) = @_;
197 ### PentSpiral rect_to_n_range(): $x1,$y1, $x2,$y2
198  
199 my $d = 0;
200 foreach my $x ($x1, $x2) {
201 $x = round_nearest ($x);
202 foreach my $y ($y1, $y2) {
203 $y = round_nearest ($y);
204  
205 my $this_d = 1 + ($y >= 0 ? abs($x) + $y
206 : $x < $y ? -($x+$y)/2
207 : $x > -$y ? ($x-$y)/2
208 : -$y);
209 ### $x
210 ### $y
211 ### $this_d
212 $d = max($d, $this_d);
213 }
214 }
215 ### $d
216 return ($self->{'n_start'},
217 $self->{'n_start'} + 5*$d*($d-1)/2 + 2);
218 }
219  
220 1;
221 __END__