PlanePath/PentSpiralSkewed.pm untested

filter: Statementstmt 0.0% 0 / 105 Branchbran 0.0% 0 / 18 Conditioncond 0.0% 0 / 6 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 25 Podpod 0.0% 0 / 7 totaltotal 0.0% 0 / 161 CC 10 SCAR 21.6
CC
10
Cov
0%
n_to_xy
30.0
xy_to_n
20.0
new
6.0
CRAP 8.7
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::PentSpiralSkewed;
20 use 5.004;
21 use strict;
22 #use List::Util 'min','max';
23 *min = \&Math::PlanePath::_min;
24 *max = \&Math::PlanePath::_max;
25  
26 use vars '$VERSION', '@ISA';
27 $VERSION = 128;
28 use Math::PlanePath;
29 *_sqrtint = \&Math::PlanePath::_sqrtint;
30 @ISA = ('Math::PlanePath');
31  
32 use Math::PlanePath::Base::Generic
33 'round_nearest';
34  
35 # uncomment this to run the ### lines
36 #use Smart::Comments;
37  
38  
39 use constant parameter_info_array =>
40 [
41 Math::PlanePath::Base::Generic::parameter_info_nstart1(),
42 ];
43  
44 use constant xy_is_visited => 1;
45  
46 sub x_negative_at_n {
47 my ($self) = @_;
48 return $self->n_start + 3;
49 }
50 sub y_negative_at_n {
51 my ($self) = @_;
52 return $self->n_start + 4;
53 }
54 sub _UNDOCUMENTED__dxdy_list_at_n {
55 my ($self) = @_;
56 return $self->n_start + 6;
57 }
58  
59 use constant dx_minimum => -1;
60 use constant dx_maximum => 1;
61 use constant dy_minimum => -1;
62 use constant dy_maximum => 1;
63 use constant 1.02 _UNDOCUMENTED__dxdy_list => (1,0, # E
64 0,1, # N
65 -1,1, # NW
66 -1,-1, # SW
67 1,-1, # SE
68 );
69 use constant dsumxy_minimum => -2; # SW diagonal
70 use constant dsumxy_maximum => 1;
71 use constant ddiffxy_minimum => -2; # NW diagonal
72 use constant ddiffxy_maximum => 2; # SE diagonal
73 use constant dir_maximum_dxdy => (1,-1); # South-East
74 use constant turn_any_right => 0; # only left or straight
75  
76  
77 #------------------------------------------------------------------------------
78  
79 sub new {
80 my $self = shift->SUPER::new(@_);
81 if (! defined $self->{'n_start'}) {
82 $self->{'n_start'} = $self->default_n_start;
83 }
84 return $self;
85 }
86  
87 sub n_to_xy {
88 my ($self, $n) = @_;
89 #### n_to_xy: $n
90  
91 # adjust to N=0 at origin X=0,Y=0
92 $n = $n - $self->{'n_start'};
93 if ($n < 0) { return; }
94  
95 my $d = int( (_sqrtint(40*$n+9)+7) / 10);
96 $n -= (5*$d-1)*$d/2;
97  
98 if ($n < -$d) {
99 $n += 2*$d;
100 if ($n < 1) {
101 # bottom horizontal
102 return ($n+$d-1, -$d+1);
103 } else {
104 # lower right vertical ...
105 return ($d, $n-$d);
106 }
107 } else {
108 if ($n <= $d) {
109 ### top diagonals left and right ...
110 return (-$n,
111 -abs($n) + $d);
112 } else {
113 ### lower left diagonal ...
114 return ($n - 2*$d,
115 -$n + $d);
116 }
117 }
118 }
119  
120 sub xy_to_n {
121 my ($self, $x, $y) = @_;
122  
123 $x = round_nearest ($x);
124 $y = round_nearest ($y);
125  
126 if ($x > 0 && $y < 0) {
127 # vertical downwards at x=0
128 # d = [ 1, 2, 3 ]
129 # n = [ 5, 14, 28 ]
130 # n = (5/2*$d**2 + 3/2*$d + 1)
131 # so
132 my $d = max($x-1, -$y);
133 ### lower right square part
134 ### $d
135 return ((5*$d + 3)*$d/2
136 + $x
137 + ($x > $d ? $y+$d : 0)
138 + $self->{'n_start'});
139 }
140  
141 # vertical at x=0
142 # d = [ 1, 2, 3 ]
143 # n = [ 3, 10, 22 ]
144 # n = (5/2*$d**2 + -1/2*$d + 1)
145 #
146 my $d = abs($x)+abs($y);
147 return ((5*$d - 1)*$d/2
148 - $x
149 + ($y < 0 ? 2*($d+$x) : 0)
150 + $self->{'n_start'});
151 }
152  
153 # not exact
154 sub rect_to_n_range {
155 my ($self, $x1,$y1, $x2,$y2) = @_;
156 ### PentSpiralSkewed rect_to_n_range(): $x1,$y1, $x2,$y2
157  
158 my $d = 0;
159 foreach my $x ($x1, $x2) {
160 $x = round_nearest ($x);
161 foreach my $y ($y1, $y2) {
162 $y = round_nearest ($y);
163  
164 my $this_d = 1 + ($x > 0 && $y < 0
165 ? max($x,-$y)
166 : abs($x)+abs($y));
167 ### $x
168 ### $y
169 ### $this_d
170 $d = max($d, $this_d);
171 }
172 }
173 ### $d
174 return ($self->{'n_start'},
175 $self->{'n_start'} + 5*$d*($d-1)/2 + 2);
176 }
177  
178 1;
179 __END__