PlanePath/TerdragonRounded.pm untested

filter: Statementstmt 0.0% 0 / 156 Branchbran 0.0% 0 / 30 Conditioncond 0.0% 0 / 6 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 29 Podpod 0.0% 0 / 9 totaltotal 0.0% 0 / 230 CC 16 SCAR 24.0
CC
16
Cov
0%
n_to_xy
30.0
xy_to_n
30.0
rect_to_n_range
20.0
CRAP 11.0
1 # Copyright 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::TerdragonRounded;
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 *_divrem_mutate = \&Math::PlanePath::_divrem_mutate;
30  
31 use Math::PlanePath::Base::Generic
32 'is_infinite',
33 'round_nearest';
34 use Math::PlanePath::Base::Digits
35 'round_up_pow';
36 use Math::PlanePath::TerdragonCurve;
37  
38 # uncomment this to run the ### lines
39 # use Smart::Comments;
40  
41  
42 use constant n_start => 0;
43  
44 *parameter_info_array # arms
45 = \&Math::PlanePath::TerdragonCurve::parameter_info_array;
46 *new = \&Math::PlanePath::TerdragonCurve::new;
47  
48 {
49 my @x_negative_at_n = (undef, 24, 7, 2, 2, 2, 2);
50 sub x_negative_at_n {
51 my ($self) = @_;
52 return $x_negative_at_n[$self->{'arms'}];
53 }
54 }
55 {
56 my @y_negative_at_n = (undef, 316, 145, 32, 11, 4, 4);
57 sub y_negative_at_n {
58 my ($self) = @_;
59 return $y_negative_at_n[$self->{'arms'}];
60 }
61 }
62 use constant sumabsxy_minimum => 2; # X=2,Y=0
63 sub rsquared_minimum {
64 my ($self) = @_;
65 return ($self->arms_count < 2
66 ? 4 # 1 arm, minimum X=2,Y=0
67 : 2); # 2 or more arms, minimum X=1,Y=1
68 }
69  
70 use constant dx_minimum => -2;
71 use constant dx_maximum => 2;
72 use constant dy_minimum => -1;
73 use constant dy_maximum => 1;
74 *_UNDOCUMENTED__dxdy_list = \&Math::PlanePath::_UNDOCUMENTED__dxdy_list_six;
75 use constant absdx_minimum => 1;
76 use constant dsumxy_minimum => -2; # diagonals
77 use constant dsumxy_maximum => 2;
78 use constant ddiffxy_minimum => -2;
79 use constant ddiffxy_maximum => 2;
80 use constant dir_maximum_dxdy => (1,-1); # South-East
81 use constant turn_any_straight => 0; # never straight
82  
83  
84 #------------------------------------------------------------------------------
85  
86 sub n_to_xy {
87 my ($self, $n) = @_;
88 ### TerdragonRounded n_to_xy(): $n
89  
90 if ($n < 0) { # negative
91 return;
92 }
93 if (is_infinite($n)) {
94 return ($n,$n);
95 }
96  
97 {
98 # ENHANCE-ME: the ends join and the direction can be had without a full
99 # N+1 calculation
100 my $int = int($n);
101 ### $int
102 ### $n
103 if ($n != $int) {
104 my ($x1,$y1) = $self->n_to_xy($int);
105 my ($x2,$y2) = $self->n_to_xy($int+$self->{'arms'});
106 my $frac = $n - $int; # inherit possible BigFloat
107 my $dx = $x2-$x1;
108 my $dy = $y2-$y1;
109 return ($frac*$dx + $x1, $frac*$dy + $y1);
110 }
111 $n = $int; # BigFloat int() gives BigInt, use that
112 }
113  
114 my $arms_count = $self->{'arms'};
115 my $arm = _divrem_mutate ($n, $arms_count);
116 my $pair = _divrem_mutate ($n, 2);
117  
118 my ($x, $y) = $self->Math::PlanePath::TerdragonCurve::n_to_xy
119 ((9*$n + ($pair ? 4 : 2)) * $arms_count + $arm);
120  
121 ### is: (($x+3*$y)/2).", ".(($y-$x)/2)
122  
123 return (($x+3*$y)/2, ($y-$x)/2); # rotate -60
124 }
125  
126 sub xy_to_n {
127 my ($self, $x, $y) = @_;
128 ### TerdragonRounded xy_to_n(): "$x, $y"
129  
130 $x = round_nearest($x);
131 $y = round_nearest($y);
132  
133 {
134 my $sum = 3*$y + $x;
135 if (is_infinite($sum)) { return $sum; }
136 $sum %= 6;
137 unless ($sum == 2 || $sum == 4) { return undef; }
138 }
139  
140 ($x,$y) = (($x-3*$y)/2, # rotate +60
141 ($x+$y)/2);
142 ### rotated: "$x,$y"
143  
144 my @n_list = $self->Math::PlanePath::TerdragonCurve::xy_to_n_list ($x, $y);
145 ### @n_list
146  
147 my $arms_count = $self->{'arms'};
148 foreach my $n (@n_list) {
149 my $arm = _divrem_mutate ($n, $arms_count);
150 ### $arm
151 ### remainder: $n
152  
153 my $mod = $n % 9;
154 if ($mod == 2) {
155 return (2*int(($n-2)/9))*$arms_count + $arm;
156 }
157 if ($mod == 4) {
158 return (2*int(($n-4)/9) + 1)*$arms_count + $arm;
159 }
160 }
161 return undef;
162 }
163  
164 # arms==6 is all "hex_centred" points X+3Y mod 6 == 2 or 4
165 sub xy_is_visited {
166 my ($self, $x, $y) = @_;
167 if ($self->{'arms'} == 6) {
168 my $mod = (3*$y + $x) % 6;
169 return ($mod == 2 || $mod == 4);
170 }
171 return defined($self->xy_to_n($x,$y));
172 }
173  
174 # not exact
175 sub rect_to_n_range {
176 my ($self, $x1,$y1, $x2,$y2) = @_;
177  
178 # my $xmax = int(max(abs($x1),abs($x2))) + 1;
179 # my $ymax = int(max(abs($y1),abs($y2))) + 1;
180 # return (0,
181 # ($xmax*$xmax + 3*$ymax*$ymax)
182 # * 1
183 # * $self->{'arms'});
184  
185 $x1 = round_nearest ($x1);
186 $y1 = round_nearest ($y1);
187 $x2 = round_nearest ($x2);
188 $y2 = round_nearest ($y2);
189  
190 ($x1,$x2) = ($x2,$x1) if $x1 > $x2;
191 ($y1,$y2) = ($y2,$y1) if $y1 > $y2;
192  
193 # FIXME: How much wider ?
194 # Might matter when TerdragonCurve becomes exact.
195 $x1 = int($x1/3) - 2;
196 $y1 = int($y1/3) - 2;
197 $x2 = int($x2/3) + 2;
198 $y2 = int($y2/3) + 2;
199  
200 my ($n_lo, $n_hi) = $self->Math::PlanePath::TerdragonCurve::rect_to_n_range
201 ($x1,$y1, $x2,$y2);
202 if ($n_hi >= $n_hi) {
203 $n_lo *= 2;
204 $n_hi = 2*$n_hi + 1;
205 }
206 return ($n_lo, $n_hi);
207 }
208  
209  
210 #-----------------------------------------------------------------------------
211 # level_to_n_range()
212  
213 # like TerdragonMidpoint but 2* points on each arm, numbered starting 0
214 #
215 sub level_to_n_range {
216 my ($self, $level) = @_;
217 return (0, (2*$self->{'arms'}) * 3**$level - 1);
218 }
219 sub n_to_level {
220 my ($self, $n) = @_;
221 if ($n < 0) { return undef; }
222 if (is_infinite($n)) { return $n; }
223 $n = round_nearest($n);
224 _divrem_mutate ($n, 2 * $self->{'arms'});
225 my ($pow, $exp) = round_up_pow ($n+1, 3);
226 return $exp;
227 }
228  
229 #-----------------------------------------------------------------------------
230 1;
231 __END__