PlanePath/DiamondArms.pm

filter: Statementstmt 99.1% 119 / 120 Branchbran 93.7% 15 / 16 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 27 / 27 Podpod 100% 3 / 3 totaltotal 98.8% 164 / 166 CC 9 SCAR 6.5
CC
9
Cov
99%
n_to_xy
5.0
xy_to_n
3.0
rect_to_n_range
3.0
CRAP 1.9
« NumSeq/OEIS/Catalogue/Plugin/PlanePath.pm ↑ Summary PlanePath/Base/NSEW.pm »
1 # Copyright 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 it
6 # under the terms of the GNU General Public License as published by the Free
7 # 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 # math-image --path=DiamondArms --lines --scale=10
20 # math-image --path=DiamondArms --all --output=numbers_dash
21 # math-image --path=DiamondArms --values=Polygonal,polygonal=8
22 #
23 # RepdigitsAnyBase fall on 14 or 15 lines ...
24 #
25  
26 package Math::PlanePath::DiamondArms;
27 1 ▶use 5.004;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
29 #use List::Util 'min', 'max';
30 1 *min = \&Math::PlanePath::_min;
31 1 *max = \&Math::PlanePath::_max;
32  
33 1 ▶use vars '$VERSION', '@ISA';
Subroutine BEGIN: called CC=1 SCAR=0.0
34 1 $VERSION = 128;
35 1 ▶use Math::PlanePath;
Subroutine BEGIN: called CC=1 SCAR=0.0
36 1 @ISA = ('Math::PlanePath');
37 1 *_divrem_mutate = \&Math::PlanePath::_divrem_mutate;
38 1 *_sqrtint = \&Math::PlanePath::_sqrtint;
39  
40 use Math::PlanePath::Base::Generic
41 1 ▶ 'round_nearest';
Subroutine BEGIN: called CC=1 SCAR=0.0
42 1 ▶use Math::PlanePath::DiamondSpiral;
Subroutine BEGIN: called CC=1 SCAR=0.0
43  
44 # uncomment this to run the ### lines
45 #use Devel::Comments;
46  
47  
48 1 ▶use constant arms_count => 4;
Subroutine BEGIN: called CC=1 SCAR=0.0
49 1 ▶use constant xy_is_visited => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
50 1 ▶use constant x_negative_at_n => 8;
Subroutine BEGIN: called CC=1 SCAR=0.0
51 1 ▶use constant y_negative_at_n => 5;
Subroutine BEGIN: called CC=1 SCAR=0.0
52  
53 1 ▶use constant dx_minimum => -1;
Subroutine BEGIN: called CC=1 SCAR=0.0
54 1 ▶use constant dx_maximum => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
55 1 ▶use constant dy_minimum => -1;
Subroutine BEGIN: called CC=1 SCAR=0.0
56 1 ▶use constant dy_maximum => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
57 1 use constant 1.02 _UNDOCUMENTED__dxdy_list => (1,1, # NE diagonals
58 -1,1, # NW
59 -1,-1, # SW
60 1 ▶ 1,-1); # SE
Subroutine BEGIN: called CC=1 SCAR=0.0
61 1 ▶use constant absdx_minimum => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
62 1 ▶use constant absdy_minimum => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
63 1 ▶use constant dsumxy_minimum => -2; # diagonals
Subroutine BEGIN: called CC=1 SCAR=0.0
64 1 ▶use constant dsumxy_maximum => 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
65 1 ▶use constant ddiffxy_minimum => -2;
Subroutine BEGIN: called CC=1 SCAR=0.0
66 1 ▶use constant ddiffxy_maximum => 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
67 1 ▶use constant dir_minimum_dxdy => (1,1); # North-East
Subroutine BEGIN: called CC=1 SCAR=0.0
68 1 ▶use constant dir_maximum_dxdy => (1,-1); # South-East
Subroutine BEGIN: called CC=1 SCAR=0.0
69 1 ▶use constant turn_any_right => 0; # only left or straight
Subroutine BEGIN: called CC=1 SCAR=0.0
70  
71  
72 #------------------------------------------------------------------------------
73 # 28
74 # 172 +144
75 # 444 +272 +128
76 # 844 +400 +128
77  
78 # [ 0, 1, 2, 3,],
79 # [ 0, 1, 3, 6 ],
80 # N = (1/2 d^2 + 1/2 d)
81 # = (1/2*$d**2 + 1/2*$d)
82 # = ($d+1)*$d/2
83 # d = -1/2 + sqrt(2 * $n + 1/4)
84 # = (-1 + sqrt(8*$n + 1))/2
85  
86 sub n_to_xy {
87 512 ▶ my ($self, $n) = @_;
Subroutine n_to_xy: called CC=5 SCAR=16.1
Pod documented
88 ### DiamondArms n_to_xy(): $n
89 512 ▶ if ($n < 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n < 1) 0 512 512
90 0 return;
91 }
92 512 $n -= 1;
93 512 my $frac;
94 {
95 512 my $int = int($n);
96 512 $frac = $n - $int;
97 512 $n = $int; # BigFloat int() gives BigInt, use that
98 }
99  
100 # arm as initial rotation
101 512 my $rot = _divrem_mutate($n,4);
102 ### $n
103  
104 # if (($rot%4) != 3) {
105 # return;
106 # }
107  
108 512 my $d = int ((-1 + _sqrtint(8*$n+1)) / 2);
109 ### d frac: ((-1 + sqrt(8*$n + 1)) / 2)
110 ### $d
111 ### base: $d*($d+1)/2
112  
113 512 $n -= $d*($d+1)/2;
114 ### remainder: $n
115 ### assert: $n <= $d
116  
117 512 my $x = ($frac + $n) - $d;
118 512 my $y = - ($frac + $n);
119 ### unrot: "$x,$y"
120  
121 512 $rot = ($rot + $d) % 4;
122 ### $rot
123  
124 512 ▶ if ($rot == 1) {
Branch: 3 branches
BranchTrueFalseTotal
if ($rot == 1) { } 127 385 512
elsif ($rot == 2) { } 107 278 385
elsif ($rot == 3) { } 124 154 278
125 127 ($x,$y) = (1-$y, $x); # rotate +90 and right
126 } elsif ($rot == 2) {
127 107 ($x,$y) = (1-$x, 1-$y); # rotate 180 and up+right
128 } elsif ($rot == 3) {
129 124 ($x,$y) = ($y, 1-$x); # rotate +90 and up
130 }
131 512 return ($x,$y);
132 }
133  
134 sub xy_to_n {
135 2556 ▶ my ($self, $x, $y) = @_;
Subroutine xy_to_n: called CC=3 SCAR=11.0
Pod documented
136 2556 $x = round_nearest ($x);
137 2556 $y = round_nearest ($y);
138 ### DiamondArms xy_to_n: "$x,$y"
139  
140 2556 my $rot = 0;
141 # eg. y=2 have (0<=>$y)-$y == -1-2 == -3
142 2556 ▶ if ($y >= ($x > 0)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($y >= ($x > 0)) 1475 1081 2556
143 ### above horizontal, rot -180 ...
144 1475 $rot = 2;
145 1475 $x = 1-$x; # rotate 180 and offset
146 1475 $y = 1-$y;
147 }
148 2556 ▶ if ($x > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x > 0) 1581 975 2556
149 ### right of vertical, rot -90 ...
150 1581 $rot++;
151 1581 ($x,$y) = ($y,1-$x); # rotate -90 and offset
152 }
153  
154 # horizontal negative X axis
155 # d = -x + -y
156 # d=0 n=1
157 # d=4 n=41
158 # d=8 n=145
159 # d=12 n=313
160 # N = (2 d^2 + 2 d + 1)
161 # = (2*$d**2 + 2*$d + 1)
162 # = ((2*$d + 2)*$d + 1)
163 #
164 2556 my $d = -$x - $y;
165 ### xy: "$x,$y"
166 ### $d
167 ### $rot
168 ### base: ((2*$d + 2)*$d + 1)
169 ### offset: -4 * $y
170 ### rot d mod: (($rot+$d+2) % 4)
171 2556 return ((2*$d + 2)*$d + 1) - 4*$y + (($rot-$d) % 4);
172 }
173  
174  
175 # d = [ 1, 2, 3, 4, 5, 6, 7, 8, 9 ],
176 # Nmax = [ 9, 25, 49, 81, 121, 169, 225, 289, 361 ]
177 # being the N=5 arm one spot before the corner of each run
178 # N = (4 d^2 + 4 d + 1)
179 # = (2d+1)^2
180 # = ((4*$d + 4)*$d + 1)
181 # or for d-1
182 # N = (4 d^2 - 4 d + 1)
183 # = (2d-1)^2
184 # = ((4*$d - 4)*$d + 1)
185 #
186 # not exact
187 sub rect_to_n_range {
188 512 ▶ my ($self, $x1,$y1, $x2,$y2) = @_;
Subroutine rect_to_n_range: called CC=3 SCAR=11.0
Pod documented
189 512 $x1 = round_nearest ($x1);
190 512 $y1 = round_nearest ($y1);
191 512 $x2 = round_nearest ($x2);
192 512 $y2 = round_nearest ($y2);
193 512 ▶ my $x = (($x1<0) == ($x2<0) ? min(abs($x1),abs($x2)) : 0);
Branch: 1 branch
BranchTrueFalseTotal
$x1 < 0 == $x2 < 0 ? : 510 2 512
194 512 ▶ my $y = (($y1<0) == ($y2<0) ? min(abs($y1),abs($y2)) : 0);
Branch: 1 branch
BranchTrueFalseTotal
$y1 < 0 == $y2 < 0 ? : 511 1 512
195 512 my $d = max(0, $x + $y - 2);
196 512 return (((2*$d + 2)*$d + 1),
197 max ($self->xy_to_n($x1,$y1),
198 $self->xy_to_n($x1,$y2),
199 $self->xy_to_n($x2,$y1),
200 $self->xy_to_n($x2,$y2)));
201 }
202  
203 1 1;
204 __END__
« NumSeq/OEIS/Catalogue/Plugin/PlanePath.pm ↑ Summary PlanePath/Base/NSEW.pm »