PlanePath/HeptSpiralSkewed.pm untested

filter: Statementstmt 0.0% 0 / 112 Branchbran 0.0% 0 / 22 Conditioncond 0.0% 0 / 6 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 25 Podpod 0.0% 0 / 7 totaltotal 0.0% 0 / 172 CC 12 SCAR 25.6
CC
12
Cov
0%
n_to_xy
42.0
xy_to_n
30.0
new
6.0
CRAP 13.0
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::HeptSpiralSkewed;
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 *_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 [
40 Math::PlanePath::Base::Generic::parameter_info_nstart1(),
41 ];
42  
43 use constant xy_is_visited => 1;
44 sub x_negative_at_n {
45 my ($self) = @_;
46 return $self->n_start + 3;
47 }
48 sub y_negative_at_n {
49 my ($self) = @_;
50 return $self->n_start + 5;
51 }
52 sub _UNDOCUMENTED__dxdy_list_at_n {
53 my ($self) = @_;
54 return $self->n_start + 8;
55 }
56 use constant dx_minimum => -1;
57 use constant dx_maximum => 1;
58 use constant dy_minimum => -1;
59 use constant dy_maximum => 1;
60 use constant 1.02 _UNDOCUMENTED__dxdy_list => (1,0, # E four plus NW
61 0,1, # N
62 -1,1, # NW
63 -1,0, # W
64 0,-1); # S
65 use constant dsumxy_minimum => -1; # W,S straight
66 use constant dsumxy_maximum => 1; # N,E straight
67 use constant ddiffxy_minimum => -2; # NW diagonal
68 use constant ddiffxy_maximum => 1;
69 use constant dir_maximum_dxdy => (0,-1); # South
70  
71 use constant turn_any_right => 0; # only left or straight
72  
73  
74 #------------------------------------------------------------------------------
75  
76 sub new {
77 my $self = shift->SUPER::new(@_);
78 if (! defined $self->{'n_start'}) {
79 $self->{'n_start'} = $self->default_n_start;
80 }
81 return $self;
82 }
83  
84 # base South-West diagonal
85 # d = [ 1, 2, 3, 4 ]
86 # n = [ 0, 5, 17, 36 ]
87 # N = (7/2 d^2 - 11/2 d + 2)
88 # = (7/2*$d**2 - 11/2*$d + 2)
89 # = ((7/2*$d - 11/2)*$d + 2)
90 # d = 11/14 + sqrt(2/7 * $n + 9/196)
91 # = (11 + 14*sqrt(2/7 * $n + 9/196))/14
92 # = (sqrt(56*$n + 9) + 11)/14
93 #
94 # split North Y axis
95 # d = [ 1, 2, 3 ]
96 # n = [ 2, 11, 27 ]
97 # N = (7*$d-3)*$d/2
98  
99 sub n_to_xy {
100 my ($self, $n) = @_;
101 #### HeptSpiralSkewed n_to_xy: $n
102  
103 $n = $n - $self->{'n_start'}; # adjust to N=0 at origin X=0,Y=0
104 if ($n < 0) { return; }
105  
106 my $d = int((_sqrtint(56*$n+9) + 11) / 14);
107 ### $d
108 ### d frac: (_sqrtint(56*$n+9) + 11) / 14
109  
110 $n -= (7*$d-3)*$d/2;
111 ### remainder: $n
112  
113 if ($n < 0) { # split at Y axis
114 if ($n >= -$d) {
115 #### right diagonal ...
116 return (-$n,
117 $n + $d);
118 }
119 $n += $d;
120 if ($n < 1-$d) {
121 ### bottom horizontal ...
122 return ($n + 2*$d-1, 1-$d);
123 }
124 ### right vertical ...
125 return ($d, $n);
126 }
127 if ($n <= $d) {
128 ### top horizontal ...
129 return (-$n, $d);
130 }
131 #### left vertical ...
132 return (-$d,
133 -$n + 2*$d);
134 }
135  
136 sub xy_to_n {
137 my ($self, $x, $y) = @_;
138 $x = round_nearest ($x);
139 $y = round_nearest ($y);
140  
141 if ($x >= 0 && $y >= 0) {
142 ### slope
143 # relative to the y=0 base same as above
144 # d = [ 1, 2, 3, 4 ]
145 # n = [ 2, 10, 25, 47 ]
146 # n = (7/2*$d**2 + -5/2*$d + 1)
147 # = (3.5*$d - 2.5)*$d + 1
148 #
149 my $d = $x + $y;
150 return (7*$d - 5)*$d/2 + $y + $self->{'n_start'};
151 }
152  
153 my $d = max(abs($x),abs($y));
154 my $n = (7*$d - 5)*$d/2;
155 if ($y == $d) {
156 ### top horizontal
157 return $n+$d - $x + $self->{'n_start'};
158 }
159 if ($y == -$d) {
160 ### bottom horizontal
161 return $n + 5*$d + $x + $self->{'n_start'};
162 }
163 if ($x == $d) {
164 ### right vertical
165 return $n + $y + $self->{'n_start'};
166 }
167 # ($x == - $d)
168 ### left vertical
169 return $n + 3*$d - $y + $self->{'n_start'};
170 }
171  
172 # not exact
173 sub rect_to_n_range {
174 my ($self, $x1,$y1, $x2,$y2) = @_;
175  
176 $x1 = round_nearest ($x1);
177 $y1 = round_nearest ($y1);
178 $x2 = round_nearest ($x2);
179 $y2 = round_nearest ($y2);
180 my $d = 0;
181 foreach my $x ($x1, $x2) {
182 foreach my $y ($y1, $y2) {
183 $d = max ($d,
184 1 + ($x > 0 && $y > 0
185 ? $x+$y # slope
186 : max(abs($x),abs($y)))); # square corners
187 }
188 }
189 # ENHANCE-ME: find actual minimum if rect doesn't cover 0,0
190 return ($self->{'n_start'},
191 $self->{'n_start'} + (7*$d - 5)*$d/2);
192 }
193  
194 1;
195 __END__