| 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 | ||||||||||||||||||||||||||||||||||||||||||
| 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 | # math-image --path=GreekKeySpiral --lines --scale=25 | ||||||||||||||||||||||||||||||||||||||||||
| 20 | # http://gwydir.demon.co.uk/jo/greekkey/corners.htm | ||||||||||||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||||||||||||||||
| 23 | package Math::PlanePath::GreekKeySpiral; | ||||||||||||||||||||||||||||||||||||||||||
| 24 | 1 | ▶ | use 5.004; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 25 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||||||||||||||||
| 27 | 1 | ▶ | use vars '$VERSION', '@ISA'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 28 | 1 | $VERSION = 128; | |||||||||||||||||||||||||||||||||||||||||
| 29 | 1 | ▶ | use Math::PlanePath; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 30 | 1 | ▶ | use Math::PlanePath::Base::NSEW; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 31 | 1 | @ISA = ('Math::PlanePath::Base::NSEW', | |||||||||||||||||||||||||||||||||||||||||
| 32 | 'Math::PlanePath'); | ||||||||||||||||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||||||||||||||||
| 34 | use Math::PlanePath::Base::Generic | ||||||||||||||||||||||||||||||||||||||||||
| 35 | 1 | 'round_nearest', | |||||||||||||||||||||||||||||||||||||||||
| 36 | 1 | ▶ | 'floor'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 37 | 1 | *_divrem = \&Math::PlanePath::_divrem; | |||||||||||||||||||||||||||||||||||||||||
| 38 | 1 | *_divrem_mutate = \&Math::PlanePath::_divrem_mutate; | |||||||||||||||||||||||||||||||||||||||||
| 39 | 1 | *_sqrtint = \&Math::PlanePath::_sqrtint; | |||||||||||||||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||||||||||||||||
| 41 | # uncomment this to run the ### lines | ||||||||||||||||||||||||||||||||||||||||||
| 42 | # use Smart::Comments; | ||||||||||||||||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||||||||||||||||
| 45 | 1 | ▶ | use constant xy_is_visited => 1; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 46 | 1 | use constant parameter_info_array => | |||||||||||||||||||||||||||||||||||||||||
| 47 | [ { name => 'turns', | ||||||||||||||||||||||||||||||||||||||||||
| 48 | share_key => 'turns_2', | ||||||||||||||||||||||||||||||||||||||||||
| 49 | display => 'Turns', | ||||||||||||||||||||||||||||||||||||||||||
| 50 | type => 'integer', | ||||||||||||||||||||||||||||||||||||||||||
| 51 | minimum => 0, | ||||||||||||||||||||||||||||||||||||||||||
| 52 | default => 2, | ||||||||||||||||||||||||||||||||||||||||||
| 53 | width => 2, | ||||||||||||||||||||||||||||||||||||||||||
| 54 | }, | ||||||||||||||||||||||||||||||||||||||||||
| 55 | 1 | ▶ | ]; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||||||||||||||||
| 57 | sub x_negative_at_n { | ||||||||||||||||||||||||||||||||||||||||||
| 58 | 0 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
x_negative_at_n: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 59 | 0 | return $self->n_start + 4*($self->{'turns'}+1)**2; | |||||||||||||||||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||||||||||||||||
| 61 | sub y_negative_at_n { | ||||||||||||||||||||||||||||||||||||||||||
| 62 | 0 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
y_negative_at_n: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 63 | 0 | return $self->n_start + 6*($self->{'turns'}+1)**2; | |||||||||||||||||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||||||||||||||||
| 66 | # 17-- 18--19--20--21 | ||||||||||||||||||||||||||||||||||||||||||
| 67 | # | | ||||||||||||||||||||||||||||||||||||||||||
| 68 | # 16 3t-2 -- 8 -- 2t | ||||||||||||||||||||||||||||||||||||||||||
| 69 | # | | | | ||||||||||||||||||||||||||||||||||||||||||
| 70 | # 15 4t-5 ---11 6 | ||||||||||||||||||||||||||||||||||||||||||
| 71 | # | | | | ||||||||||||||||||||||||||||||||||||||||||
| 72 | # 14-- 13-----12 5 | ||||||||||||||||||||||||||||||||||||||||||
| 73 | # | | ||||||||||||||||||||||||||||||||||||||||||
| 74 | # 1---- 2----- 3-- t | ||||||||||||||||||||||||||||||||||||||||||
| 75 | # | ||||||||||||||||||||||||||||||||||||||||||
| 76 | sub _UNDOCUMENTED__dxdy_list_at_n { | ||||||||||||||||||||||||||||||||||||||||||
| 77 | 0 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_UNDOCUMENTED__dxdy_list_at_n: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||||||||||||||||
| 78 | 0 | my $turns = $self->{'turns'}; | |||||||||||||||||||||||||||||||||||||||||
| 79 | 0 | ▶ | return $self->n_start + ($turns == 0 ? 4 # turns=0 | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 80 | : $turns <= 2 ? 6 # turns=1,2 | ||||||||||||||||||||||||||||||||||||||||||
| 81 | : 3*$turns - 4); | ||||||||||||||||||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||||||||||||||||
| 84 | sub turn_any_right { | ||||||||||||||||||||||||||||||||||||||||||
| 85 | 0 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
turn_any_right: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 86 | 0 | return ($self->{'turns'} != 0); # SquareSpiral is left or straight only | |||||||||||||||||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||||||||||||||||
| 88 | sub _UNDOCUMENTED__turn_any_right_at_n { | ||||||||||||||||||||||||||||||||||||||||||
| 89 | 0 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_UNDOCUMENTED__turn_any_right_at_n: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||||||||||||||||
| 90 | # turns=1 2,4,7,11,22,29 | ||||||||||||||||||||||||||||||||||||||||||
| 91 | return ($self->{'turns'} == 0 ? undef # SquareSpiral left or straight only | ||||||||||||||||||||||||||||||||||||||||||
| 92 | 0 | ▶ | : $self->n_start + $self->{'midpoint'}-1); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||||||||||||||||
| 94 | sub _UNDOCUMENTED__turn_any_left_at_n { | ||||||||||||||||||||||||||||||||||||||||||
| 95 | 0 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_UNDOCUMENTED__turn_any_left_at_n: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||||||||||||||||
| 96 | 0 | my $turns = $self->{'turns'}; | |||||||||||||||||||||||||||||||||||||||||
| 97 | # turns=1 2,4,7,11,22,29 | ||||||||||||||||||||||||||||||||||||||||||
| 98 | 0 | ▶ | return $self->n_start + ($turns==0 ? 1 | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 99 | : $turns==1 ? 3 | ||||||||||||||||||||||||||||||||||||||||||
| 100 | : $turns-1); | ||||||||||||||||||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||||||||||||||||
| 104 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||||||||||||||||
| 106 | # turns=1 | ||||||||||||||||||||||||||||||||||||||||||
| 107 | # 2---3 | ||||||||||||||||||||||||||||||||||||||||||
| 108 | # | | | ||||||||||||||||||||||||||||||||||||||||||
| 109 | # 0---1 4 | ||||||||||||||||||||||||||||||||||||||||||
| 110 | # | ||||||||||||||||||||||||||||||||||||||||||
| 111 | # turns=2 | | ||||||||||||||||||||||||||||||||||||||||||
| 112 | # 5---6---7 18 15--14 | ||||||||||||||||||||||||||||||||||||||||||
| 113 | # | | | | | | ||||||||||||||||||||||||||||||||||||||||||
| 114 | # 4---3 8 17--16 13 x=1,y=1 | ||||||||||||||||||||||||||||||||||||||||||
| 115 | # | | | | ||||||||||||||||||||||||||||||||||||||||||
| 116 | # 0---1---2 9 10--11--12 | ||||||||||||||||||||||||||||||||||||||||||
| 117 | # | ||||||||||||||||||||||||||||||||||||||||||
| 118 | # turns=3 | ||||||||||||||||||||||||||||||||||||||||||
| 119 | # 10--11--12--13 | ||||||||||||||||||||||||||||||||||||||||||
| 120 | # | | | ||||||||||||||||||||||||||||||||||||||||||
| 121 | # 9 6---5 14 x=2,y=1 | ||||||||||||||||||||||||||||||||||||||||||
| 122 | # | | | | | ||||||||||||||||||||||||||||||||||||||||||
| 123 | # 8---7 4 15 | ||||||||||||||||||||||||||||||||||||||||||
| 124 | # | | | ||||||||||||||||||||||||||||||||||||||||||
| 125 | # 0---1---2---3 16 | ||||||||||||||||||||||||||||||||||||||||||
| 126 | # | ||||||||||||||||||||||||||||||||||||||||||
| 127 | # turns=4 | ||||||||||||||||||||||||||||||||||||||||||
| 128 | # 17--18--19--20--21 50 37--36--35--34 | ||||||||||||||||||||||||||||||||||||||||||
| 129 | # | | | | | 3,3,2,1,1,1,2,3,4,down4 | ||||||||||||||||||||||||||||||||||||||||||
| 130 | # 16 9---8---7 22 49 38 41--42 33 | ||||||||||||||||||||||||||||||||||||||||||
| 131 | # | | | | | | | | | | ||||||||||||||||||||||||||||||||||||||||||
| 132 | # 15 10--11 6 23 48 39--40 43 32 x=3,y=2 | ||||||||||||||||||||||||||||||||||||||||||
| 133 | # | | | | | | | | ||||||||||||||||||||||||||||||||||||||||||
| 134 | # 14--13--12 5 24 47--46--45--44 31 | ||||||||||||||||||||||||||||||||||||||||||
| 135 | # | | | | ||||||||||||||||||||||||||||||||||||||||||
| 136 | # 0---1---2---3---4 25--26--27--28--29--30 5,4,3,2,1,1,1,2,3,up3 | ||||||||||||||||||||||||||||||||||||||||||
| 137 | # | ||||||||||||||||||||||||||||||||||||||||||
| 138 | # turns=5 | ||||||||||||||||||||||||||||||||||||||||||
| 139 | # 26--27--28--29--30--31 | ||||||||||||||||||||||||||||||||||||||||||
| 140 | # | | 4,4,3,2,1,1,1,2,3,4,5,5 | ||||||||||||||||||||||||||||||||||||||||||
| 141 | # 25 12--11--10---9 32 | ||||||||||||||||||||||||||||||||||||||||||
| 142 | # | | | | | ||||||||||||||||||||||||||||||||||||||||||
| 143 | # 24 13 16--17 8 33 5,4,3,2,1,1,1,2,3,4,5,rem | ||||||||||||||||||||||||||||||||||||||||||
| 144 | # | | | | | | | | ||||||||||||||||||||||||||||||||||||||||||
| 145 | # 35 23 14--15 18 7 34 | ||||||||||||||||||||||||||||||||||||||||||
| 146 | # | | | | | x=3,y=3 | ||||||||||||||||||||||||||||||||||||||||||
| 147 | # 36 22--21--20--19 6 35 | ||||||||||||||||||||||||||||||||||||||||||
| 148 | # | | | ||||||||||||||||||||||||||||||||||||||||||
| 149 | # 0---1---2---3---4---5 36- | ||||||||||||||||||||||||||||||||||||||||||
| 150 | # | ||||||||||||||||||||||||||||||||||||||||||
| 151 | # turns=6 | ||||||||||||||||||||||||||||||||||||||||||
| 152 | # 37--38--39--40--41--42--43 | ||||||||||||||||||||||||||||||||||||||||||
| 153 | # | | | ||||||||||||||||||||||||||||||||||||||||||
| 154 | # 36 15--14--13--12--11 44 x=3,y=3 | ||||||||||||||||||||||||||||||||||||||||||
| 155 | # | | | | | ||||||||||||||||||||||||||||||||||||||||||
| 156 | # 35 16 23--24--25 10 45 | ||||||||||||||||||||||||||||||||||||||||||
| 157 | # | | | | | | | ||||||||||||||||||||||||||||||||||||||||||
| 158 | # 34 17 22--21 26 9 46 6,5,4,3,2,1,1,1,2,3,4,5,rem | ||||||||||||||||||||||||||||||||||||||||||
| 159 | # | | | | | | | ||||||||||||||||||||||||||||||||||||||||||
| 160 | # 33 18--19--20 27 8 47 | ||||||||||||||||||||||||||||||||||||||||||
| 161 | # | | | | | ||||||||||||||||||||||||||||||||||||||||||
| 162 | # 32--31--30--29--28 7 48 | ||||||||||||||||||||||||||||||||||||||||||
| 163 | # | | | ||||||||||||||||||||||||||||||||||||||||||
| 164 | # 0---1---2---3---4---5---6 49- | ||||||||||||||||||||||||||||||||||||||||||
| 165 | # | ||||||||||||||||||||||||||||||||||||||||||
| 166 | # turns=7 | ||||||||||||||||||||||||||||||||||||||||||
| 167 | # 50--51--52--53--54--55--56--57 | ||||||||||||||||||||||||||||||||||||||||||
| 168 | # | | | ||||||||||||||||||||||||||||||||||||||||||
| 169 | # 49 18--17--16--15--14--13 58 | ||||||||||||||||||||||||||||||||||||||||||
| 170 | # | | | | | ||||||||||||||||||||||||||||||||||||||||||
| 171 | # 48 19 32--33--34--35 12 59 x=4,y=3 | ||||||||||||||||||||||||||||||||||||||||||
| 172 | # | | | | | | | ||||||||||||||||||||||||||||||||||||||||||
| 173 | # 47 20 31 28--27 36 11 60 | ||||||||||||||||||||||||||||||||||||||||||
| 174 | # | | | | | | | | | ||||||||||||||||||||||||||||||||||||||||||
| 175 | # 46 21 30--29 26 37 10 61 6,5,4,3,2,1,1,1,2,3,4,5,rem | ||||||||||||||||||||||||||||||||||||||||||
| 176 | # | | | | | | | ||||||||||||||||||||||||||||||||||||||||||
| 177 | # 45 22--23--24--25 38 9 62 | ||||||||||||||||||||||||||||||||||||||||||
| 178 | # | | | | | ||||||||||||||||||||||||||||||||||||||||||
| 179 | # 44--43--42--41--40--39 8 63 | ||||||||||||||||||||||||||||||||||||||||||
| 180 | # | | | ||||||||||||||||||||||||||||||||||||||||||
| 181 | # 0---1---2---3---4---5---6---7 64 | ||||||||||||||||||||||||||||||||||||||||||
| 182 | # | ||||||||||||||||||||||||||||||||||||||||||
| 183 | # turns=8 x=5,y=4 | ||||||||||||||||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||||||||||||||||
| 186 | # centre | ||||||||||||||||||||||||||||||||||||||||||
| 187 | # 2 1 1 | ||||||||||||||||||||||||||||||||||||||||||
| 188 | # 3 2 1 | ||||||||||||||||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||||||||||||||||
| 190 | # 4 3 2 | ||||||||||||||||||||||||||||||||||||||||||
| 191 | # 5 3 3 | ||||||||||||||||||||||||||||||||||||||||||
| 192 | # 6 3 3 | ||||||||||||||||||||||||||||||||||||||||||
| 193 | # 7 4 3 | ||||||||||||||||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||||||||||||||||
| 195 | # 8 5 4 | ||||||||||||||||||||||||||||||||||||||||||
| 196 | # 9 5 5 | ||||||||||||||||||||||||||||||||||||||||||
| 197 | # 10 5 5 | ||||||||||||||||||||||||||||||||||||||||||
| 198 | # 11 6 5 | ||||||||||||||||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||||||||||||||||
| 200 | # 12 7 6 | ||||||||||||||||||||||||||||||||||||||||||
| 201 | # 13 7 7 | ||||||||||||||||||||||||||||||||||||||||||
| 202 | # 14 7 7 | ||||||||||||||||||||||||||||||||||||||||||
| 203 | # 15 8 7 | ||||||||||||||||||||||||||||||||||||||||||
| 204 | # | ||||||||||||||||||||||||||||||||||||||||||
| 205 | # turns 2, 3, 4, 5 | ||||||||||||||||||||||||||||||||||||||||||
| 206 | # midp 4 6, 10, 15, 21 N = (1/2 d^2 + 1/2 d) | ||||||||||||||||||||||||||||||||||||||||||
| 207 | # | ||||||||||||||||||||||||||||||||||||||||||
| 208 | # 63, 189, 387, 657 | ||||||||||||||||||||||||||||||||||||||||||
| 209 | # 9*7 9*21, 9*43, 9*73 | ||||||||||||||||||||||||||||||||||||||||||
| 210 | # | ||||||||||||||||||||||||||||||||||||||||||
| 211 | # 82 226 442 | ||||||||||||||||||||||||||||||||||||||||||
| 212 | # 9*9+1 9*25+1 9*49+1 | ||||||||||||||||||||||||||||||||||||||||||
| 213 | |||||||||||||||||||||||||||||||||||||||||||
| 214 | sub new { | ||||||||||||||||||||||||||||||||||||||||||
| 215 | 26 | ▶ | my $self = shift->SUPER::new (@_); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 216 | |||||||||||||||||||||||||||||||||||||||||||
| 217 | 26 | my $turns = $self->{'turns'}; | |||||||||||||||||||||||||||||||||||||||||
| 218 | 26 | ▶ | if (! defined $turns) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 219 | 2 | $turns = 2; | |||||||||||||||||||||||||||||||||||||||||
| 220 | } elsif ($turns < 0) { | ||||||||||||||||||||||||||||||||||||||||||
| 221 | } | ||||||||||||||||||||||||||||||||||||||||||
| 222 | 26 | $self->{'turns'} = $turns; | |||||||||||||||||||||||||||||||||||||||||
| 223 | 26 | my $t1 = $turns + 1; | |||||||||||||||||||||||||||||||||||||||||
| 224 | |||||||||||||||||||||||||||||||||||||||||||
| 225 | 26 | ▶ | if (! defined $self->{'n_start'}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 226 | 26 | $self->{'n_start'} = $self->default_n_start; | |||||||||||||||||||||||||||||||||||||||||
| 227 | } | ||||||||||||||||||||||||||||||||||||||||||
| 228 | |||||||||||||||||||||||||||||||||||||||||||
| 229 | 26 | $self->{'centre_x'} = int($t1/2) + (($turns%4)==0); | |||||||||||||||||||||||||||||||||||||||||
| 230 | 26 | $self->{'centre_y'} = int($turns/2) + (($turns%4)==1); | |||||||||||||||||||||||||||||||||||||||||
| 231 | |||||||||||||||||||||||||||||||||||||||||||
| 232 | 26 | $self->{'midpoint'} = $turns*$t1/2 + 1; | |||||||||||||||||||||||||||||||||||||||||
| 233 | 26 | $self->{'side'} = $t1; | |||||||||||||||||||||||||||||||||||||||||
| 234 | 26 | $self->{'squared'} = $t1*$t1; | |||||||||||||||||||||||||||||||||||||||||
| 235 | |||||||||||||||||||||||||||||||||||||||||||
| 236 | ### turns : $self->{'turns'} | ||||||||||||||||||||||||||||||||||||||||||
| 237 | ### midpoint: $self->{'midpoint'} | ||||||||||||||||||||||||||||||||||||||||||
| 238 | ### side : $self->{'side'} | ||||||||||||||||||||||||||||||||||||||||||
| 239 | ### squared : $self->{'squared'} | ||||||||||||||||||||||||||||||||||||||||||
| 240 | |||||||||||||||||||||||||||||||||||||||||||
| 241 | 26 | return $self; | |||||||||||||||||||||||||||||||||||||||||
| 242 | } | ||||||||||||||||||||||||||||||||||||||||||
| 243 | |||||||||||||||||||||||||||||||||||||||||||
| 244 | sub n_to_xy { | ||||||||||||||||||||||||||||||||||||||||||
| 245 | 75807 | ▶ | my ($self, $n) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
n_to_xy: called CC=21 SCAR=30.4
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 246 | #### GreekKeySpiral n_to_xy: $n | ||||||||||||||||||||||||||||||||||||||||||
| 247 | |||||||||||||||||||||||||||||||||||||||||||
| 248 | 75807 | $n = $n - $self->{'n_start'}; | |||||||||||||||||||||||||||||||||||||||||
| 249 | ### n zero based: $n | ||||||||||||||||||||||||||||||||||||||||||
| 250 | 75807 | ▶ | if ($n < 0) { return; } | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 251 | |||||||||||||||||||||||||||||||||||||||||||
| 252 | 75807 | my $turns = $self->{'turns'}; | |||||||||||||||||||||||||||||||||||||||||
| 253 | 75807 | my $squared = $self->{'squared'}; | |||||||||||||||||||||||||||||||||||||||||
| 254 | 75807 | my $side = $turns + 1; | |||||||||||||||||||||||||||||||||||||||||
| 255 | |||||||||||||||||||||||||||||||||||||||||||
| 256 | ### sqrt of: ($n-1) / $squared | ||||||||||||||||||||||||||||||||||||||||||
| 257 | |||||||||||||||||||||||||||||||||||||||||||
| 258 | 75807 | my $d = _sqrtint($n / $squared); | |||||||||||||||||||||||||||||||||||||||||
| 259 | 75807 | $n -= $squared*$d*$d - 1; | |||||||||||||||||||||||||||||||||||||||||
| 260 | 75807 | my $dhalf = int($d/2); | |||||||||||||||||||||||||||||||||||||||||
| 261 | |||||||||||||||||||||||||||||||||||||||||||
| 262 | ### $d | ||||||||||||||||||||||||||||||||||||||||||
| 263 | ### $dhalf | ||||||||||||||||||||||||||||||||||||||||||
| 264 | ### n remainder: $n | ||||||||||||||||||||||||||||||||||||||||||
| 265 | |||||||||||||||||||||||||||||||||||||||||||
| 266 | 75807 | my ($x,$y); | |||||||||||||||||||||||||||||||||||||||||
| 267 | 75807 | my $square_rot = 0; | |||||||||||||||||||||||||||||||||||||||||
| 268 | 75807 | my $frac; | |||||||||||||||||||||||||||||||||||||||||
| 269 | 75807 | { my $int = int($n); | |||||||||||||||||||||||||||||||||||||||||
| 270 | 75807 | $frac = $n - int($n); | |||||||||||||||||||||||||||||||||||||||||
| 271 | 75807 | $n = $int; | |||||||||||||||||||||||||||||||||||||||||
| 272 | } | ||||||||||||||||||||||||||||||||||||||||||
| 273 | ### $frac | ||||||||||||||||||||||||||||||||||||||||||
| 274 | ### $n | ||||||||||||||||||||||||||||||||||||||||||
| 275 | |||||||||||||||||||||||||||||||||||||||||||
| 276 | 75807 | ▶ | if ($d % 2) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 277 | ### odd d, right and top ... | ||||||||||||||||||||||||||||||||||||||||||
| 278 | 35654 | ▶ | if ($n >= $squared*($d+1)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 279 | ### top ... | ||||||||||||||||||||||||||||||||||||||||||
| 280 | 15217 | $n -= $squared*2*$d; | |||||||||||||||||||||||||||||||||||||||||
| 281 | 15217 | (my $q, $n) = _divrem ($n, $squared); | |||||||||||||||||||||||||||||||||||||||||
| 282 | 15217 | $x = (-$dhalf-$q)*$side + 1; | |||||||||||||||||||||||||||||||||||||||||
| 283 | 15217 | $y = ($dhalf+1)*$side; | |||||||||||||||||||||||||||||||||||||||||
| 284 | 15217 | $square_rot = 2; | |||||||||||||||||||||||||||||||||||||||||
| 285 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 286 | ### right ... | ||||||||||||||||||||||||||||||||||||||||||
| 287 | 20437 | (my $q, $n) = _divrem ($n-$turns-1 + $squared, $squared); | |||||||||||||||||||||||||||||||||||||||||
| 288 | 20437 | $x = ($dhalf+1)*$side; | |||||||||||||||||||||||||||||||||||||||||
| 289 | 20437 | $y = ($q-$dhalf-1)*$side; | |||||||||||||||||||||||||||||||||||||||||
| 290 | 20437 | $square_rot = 1; | |||||||||||||||||||||||||||||||||||||||||
| 291 | } | ||||||||||||||||||||||||||||||||||||||||||
| 292 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 293 | ### even d, left and bottom ... | ||||||||||||||||||||||||||||||||||||||||||
| 294 | 40153 | ▶ | if ($d == 0 || $n >= $squared*($d+1)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$d == 0 or $n >= $squared * ($d + 1)
MC/DC100%2/2
$d == 0 or $n >= $squared * ($d + 1)
$d == 0
$n >= $squared * ($d + 1)
Truth table100%3/3
A or B
A= $d == 0B= $n >= $squared * ($d + 1)
| |||||||||||||||||||||||||||||||||||||||||||
| 295 | ### bottom ... | ||||||||||||||||||||||||||||||||||||||||||
| 296 | 18725 | $n -= $squared*2*$d; | |||||||||||||||||||||||||||||||||||||||||
| 297 | 18725 | (my $q, $n) = _divrem ($n, $squared); | |||||||||||||||||||||||||||||||||||||||||
| 298 | 18725 | $x = ($dhalf+$q)*$side-1; | |||||||||||||||||||||||||||||||||||||||||
| 299 | 18725 | $y = -($dhalf)*$side; | |||||||||||||||||||||||||||||||||||||||||
| 300 | 18725 | $square_rot = 0; | |||||||||||||||||||||||||||||||||||||||||
| 301 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 302 | ### left ... | ||||||||||||||||||||||||||||||||||||||||||
| 303 | 21428 | (my $q, $n) = _divrem ($n-$turns-1 + $squared, $squared); | |||||||||||||||||||||||||||||||||||||||||
| 304 | 21428 | $x = -($dhalf)*$side; | |||||||||||||||||||||||||||||||||||||||||
| 305 | 21428 | $y = -($q-$dhalf-1)*$side; | |||||||||||||||||||||||||||||||||||||||||
| 306 | 21428 | $square_rot = 3; | |||||||||||||||||||||||||||||||||||||||||
| 307 | } | ||||||||||||||||||||||||||||||||||||||||||
| 308 | } | ||||||||||||||||||||||||||||||||||||||||||
| 309 | |||||||||||||||||||||||||||||||||||||||||||
| 310 | ### assert: ! ($n < 0) | ||||||||||||||||||||||||||||||||||||||||||
| 311 | ### assert: ! ($n >= $squared) | ||||||||||||||||||||||||||||||||||||||||||
| 312 | |||||||||||||||||||||||||||||||||||||||||||
| 313 | 75807 | my $rot = $turns; | |||||||||||||||||||||||||||||||||||||||||
| 314 | 75807 | my $kx = 0; | |||||||||||||||||||||||||||||||||||||||||
| 315 | 75807 | my $ky = 0; | |||||||||||||||||||||||||||||||||||||||||
| 316 | 75807 | my $before; | |||||||||||||||||||||||||||||||||||||||||
| 317 | ### n-midpoint: $n - $self->{'midpoint'} | ||||||||||||||||||||||||||||||||||||||||||
| 318 | |||||||||||||||||||||||||||||||||||||||||||
| 319 | 75807 | ▶ | if (($n -= $self->{'midpoint'}) >= 0) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 320 | ### after middle ... | ||||||||||||||||||||||||||||||||||||||||||
| 321 | } elsif ($n += 1) { | ||||||||||||||||||||||||||||||||||||||||||
| 322 | ### before middle ... | ||||||||||||||||||||||||||||||||||||||||||
| 323 | 33092 | $n = -$n; | |||||||||||||||||||||||||||||||||||||||||
| 324 | 33092 | ▶ | if ($frac) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 325 | ### fraction ... | ||||||||||||||||||||||||||||||||||||||||||
| 326 | 3510 | $frac = 1-$frac; | |||||||||||||||||||||||||||||||||||||||||
| 327 | 3510 | $n -= 1; | |||||||||||||||||||||||||||||||||||||||||
| 328 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 329 | ### integer ... | ||||||||||||||||||||||||||||||||||||||||||
| 330 | 29582 | $n -= 0; | |||||||||||||||||||||||||||||||||||||||||
| 331 | } | ||||||||||||||||||||||||||||||||||||||||||
| 332 | 33092 | $rot += 2; | |||||||||||||||||||||||||||||||||||||||||
| 333 | 33092 | $before = 1; | |||||||||||||||||||||||||||||||||||||||||
| 334 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 335 | ### centre segment ... | ||||||||||||||||||||||||||||||||||||||||||
| 336 | 4200 | $rot += 1; | |||||||||||||||||||||||||||||||||||||||||
| 337 | 4200 | $before = 1; | |||||||||||||||||||||||||||||||||||||||||
| 338 | } | ||||||||||||||||||||||||||||||||||||||||||
| 339 | ### key n: $n | ||||||||||||||||||||||||||||||||||||||||||
| 340 | |||||||||||||||||||||||||||||||||||||||||||
| 341 | # d: [ 0, 1, 2 ] | ||||||||||||||||||||||||||||||||||||||||||
| 342 | # n: [ 0, 3, 10 ] | ||||||||||||||||||||||||||||||||||||||||||
| 343 | # d = -1/4 + sqrt(1/2 * $n + 1/16) | ||||||||||||||||||||||||||||||||||||||||||
| 344 | # = (-1 + sqrt(8*$n + 1)) / 4 | ||||||||||||||||||||||||||||||||||||||||||
| 345 | # N = (2*$d + 1)*$d | ||||||||||||||||||||||||||||||||||||||||||
| 346 | # rel = (2*$d + 1)*$d + 2*$d+1 | ||||||||||||||||||||||||||||||||||||||||||
| 347 | # = (2*$d + 3)*$d + 1 | ||||||||||||||||||||||||||||||||||||||||||
| 348 | # | ||||||||||||||||||||||||||||||||||||||||||
| 349 | 75807 | $d = int( (_sqrtint(8*$n+1) - 1)/4 ); | |||||||||||||||||||||||||||||||||||||||||
| 350 | 75807 | $n -= (2*$d+3)*$d + 1; | |||||||||||||||||||||||||||||||||||||||||
| 351 | ### $d | ||||||||||||||||||||||||||||||||||||||||||
| 352 | ### key signed rem: $n | ||||||||||||||||||||||||||||||||||||||||||
| 353 | |||||||||||||||||||||||||||||||||||||||||||
| 354 | 75807 | ▶ | if ($n < 0) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 355 | ### key vertical ... | ||||||||||||||||||||||||||||||||||||||||||
| 356 | 40030 | $kx += $d; | |||||||||||||||||||||||||||||||||||||||||
| 357 | 40030 | $ky = -$frac-$n-$d - 1 + $ky; | |||||||||||||||||||||||||||||||||||||||||
| 358 | 40030 | ▶ | if ($d % 2) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 359 | ### key right ... | ||||||||||||||||||||||||||||||||||||||||||
| 360 | 17692 | $rot += 2; | |||||||||||||||||||||||||||||||||||||||||
| 361 | 17692 | $kx += 1; | |||||||||||||||||||||||||||||||||||||||||
| 362 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 363 | } | ||||||||||||||||||||||||||||||||||||||||||
| 364 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 365 | ### key horizontal ... | ||||||||||||||||||||||||||||||||||||||||||
| 366 | 35777 | $kx = $frac+$n-$d + $kx; | |||||||||||||||||||||||||||||||||||||||||
| 367 | 35777 | $ky += $d + 1; | |||||||||||||||||||||||||||||||||||||||||
| 368 | 35777 | $rot += 2; | |||||||||||||||||||||||||||||||||||||||||
| 369 | 35777 | ▶ | if ($d % 2) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 370 | ### key bottom ... | ||||||||||||||||||||||||||||||||||||||||||
| 371 | 18174 | $rot += 2; | |||||||||||||||||||||||||||||||||||||||||
| 372 | 18174 | $kx += -1; | |||||||||||||||||||||||||||||||||||||||||
| 373 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 374 | } | ||||||||||||||||||||||||||||||||||||||||||
| 375 | } | ||||||||||||||||||||||||||||||||||||||||||
| 376 | ### kxy raw: "$kx, $ky" | ||||||||||||||||||||||||||||||||||||||||||
| 377 | |||||||||||||||||||||||||||||||||||||||||||
| 378 | 75807 | ▶ | if ($rot & 2) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 379 | 35963 | $kx = -$kx; | |||||||||||||||||||||||||||||||||||||||||
| 380 | 35963 | $ky = -$ky; | |||||||||||||||||||||||||||||||||||||||||
| 381 | } | ||||||||||||||||||||||||||||||||||||||||||
| 382 | 75807 | ▶ | if ($rot & 1) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 383 | 14114 | ($kx,$ky) = (-$ky,$kx); | |||||||||||||||||||||||||||||||||||||||||
| 384 | } | ||||||||||||||||||||||||||||||||||||||||||
| 385 | ### kxy rotated: "$kx,$ky" | ||||||||||||||||||||||||||||||||||||||||||
| 386 | |||||||||||||||||||||||||||||||||||||||||||
| 387 | 75807 | ▶ | if ($before) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 388 | 37292 | ▶ | if (($turns % 4) == 0) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 389 | 30323 | $kx -= 1; | |||||||||||||||||||||||||||||||||||||||||
| 390 | } | ||||||||||||||||||||||||||||||||||||||||||
| 391 | 37292 | ▶ | if (($turns % 4) == 1) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 392 | 3935 | $ky -= 1; | |||||||||||||||||||||||||||||||||||||||||
| 393 | } | ||||||||||||||||||||||||||||||||||||||||||
| 394 | 37292 | ▶ | if (($turns % 4) == 2) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 395 | 1388 | $kx += 1; | |||||||||||||||||||||||||||||||||||||||||
| 396 | } | ||||||||||||||||||||||||||||||||||||||||||
| 397 | 37292 | ▶ | if (($turns % 4) == 3) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 398 | 1646 | $ky += 1; | |||||||||||||||||||||||||||||||||||||||||
| 399 | } | ||||||||||||||||||||||||||||||||||||||||||
| 400 | } | ||||||||||||||||||||||||||||||||||||||||||
| 401 | |||||||||||||||||||||||||||||||||||||||||||
| 402 | 75807 | $kx += $self->{'centre_x'}; | |||||||||||||||||||||||||||||||||||||||||
| 403 | 75807 | $ky += $self->{'centre_y'}; | |||||||||||||||||||||||||||||||||||||||||
| 404 | |||||||||||||||||||||||||||||||||||||||||||
| 405 | 75807 | ▶ | if ($square_rot & 2) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 406 | 36645 | $kx = $turns-$kx; | |||||||||||||||||||||||||||||||||||||||||
| 407 | 36645 | $ky = $turns-$ky; | |||||||||||||||||||||||||||||||||||||||||
| 408 | } | ||||||||||||||||||||||||||||||||||||||||||
| 409 | 75807 | ▶ | if ($square_rot & 1) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 410 | 41865 | ($kx,$ky) = ($turns-$ky,$kx); | |||||||||||||||||||||||||||||||||||||||||
| 411 | } | ||||||||||||||||||||||||||||||||||||||||||
| 412 | |||||||||||||||||||||||||||||||||||||||||||
| 413 | # kx,ky first to inherit BigRat etc from $frac | ||||||||||||||||||||||||||||||||||||||||||
| 414 | 75807 | return ($kx + $x, | |||||||||||||||||||||||||||||||||||||||||
| 415 | $ky + $y); | ||||||||||||||||||||||||||||||||||||||||||
| 416 | } | ||||||||||||||||||||||||||||||||||||||||||
| 417 | |||||||||||||||||||||||||||||||||||||||||||
| 418 | |||||||||||||||||||||||||||||||||||||||||||
| 419 | # t+(t-1)+(t-2)+(t-3) = 4t-6 | ||||||||||||||||||||||||||||||||||||||||||
| 420 | |||||||||||||||||||||||||||||||||||||||||||
| 421 | # y=0 0 | ||||||||||||||||||||||||||||||||||||||||||
| 422 | # y=2 0+1+2+3 total 6 | ||||||||||||||||||||||||||||||||||||||||||
| 423 | # y=4 4+5+6+7 total 28 | ||||||||||||||||||||||||||||||||||||||||||
| 424 | # (2 d^2 - d) | ||||||||||||||||||||||||||||||||||||||||||
| 425 | # N=4*t*y/2 - (2y-1)*y | ||||||||||||||||||||||||||||||||||||||||||
| 426 | # =(2t - 2y + 1)*y | ||||||||||||||||||||||||||||||||||||||||||
| 427 | |||||||||||||||||||||||||||||||||||||||||||
| 428 | # x=1 0+1+2 total 3 | ||||||||||||||||||||||||||||||||||||||||||
| 429 | # x=3 3+4+5+6 total 21 | ||||||||||||||||||||||||||||||||||||||||||
| 430 | # x=5 7+8+9+10 total 55 | ||||||||||||||||||||||||||||||||||||||||||
| 431 | # (2 d^2 + d) | ||||||||||||||||||||||||||||||||||||||||||
| 432 | # N = 4*t*(x-1)/2 + 3t-3 - (2x+1)*x | ||||||||||||||||||||||||||||||||||||||||||
| 433 | # = 2*t*(x-1) + 3t-3 - (2x+1)*x | ||||||||||||||||||||||||||||||||||||||||||
| 434 | # = 2tx-2t + 3t-3 - (2x+1)*x | ||||||||||||||||||||||||||||||||||||||||||
| 435 | # = (2t-2x-1)x - 2t + 3t-3 | ||||||||||||||||||||||||||||||||||||||||||
| 436 | # = (2t-2x-1)x + t-3 | ||||||||||||||||||||||||||||||||||||||||||
| 437 | |||||||||||||||||||||||||||||||||||||||||||
| 438 | # y=0 squared-t-t total 0 | ||||||||||||||||||||||||||||||||||||||||||
| 439 | # y=2 - (t-1)-(t-2)-(t-3)-(t-4) total 10 | ||||||||||||||||||||||||||||||||||||||||||
| 440 | # y=4 - 5+6+7+8 total 36 | ||||||||||||||||||||||||||||||||||||||||||
| 441 | # (2 d^2 + d) | ||||||||||||||||||||||||||||||||||||||||||
| 442 | # N = squared - 4*t*y/2 - 2t - (2y+1)*y +(x-y) | ||||||||||||||||||||||||||||||||||||||||||
| 443 | # = squared - (2t+2y+1)*y - 2t + x | ||||||||||||||||||||||||||||||||||||||||||
| 444 | |||||||||||||||||||||||||||||||||||||||||||
| 445 | sub xy_to_n { | ||||||||||||||||||||||||||||||||||||||||||
| 446 | 59697 | ▶ | my ($self, $x, $y) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
xy_to_n: called CC=15 SCAR=27.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 447 | |||||||||||||||||||||||||||||||||||||||||||
| 448 | 59697 | $x = round_nearest ($x); | |||||||||||||||||||||||||||||||||||||||||
| 449 | 59697 | $y = round_nearest ($y); | |||||||||||||||||||||||||||||||||||||||||
| 450 | ### xy_to_n: "x=$x, y=$y" | ||||||||||||||||||||||||||||||||||||||||||
| 451 | |||||||||||||||||||||||||||||||||||||||||||
| 452 | 59697 | my $turns = $self->{'turns'}; | |||||||||||||||||||||||||||||||||||||||||
| 453 | 59697 | my $side = $turns + 1; | |||||||||||||||||||||||||||||||||||||||||
| 454 | 59697 | my $squared = $self->{'squared'}; | |||||||||||||||||||||||||||||||||||||||||
| 455 | |||||||||||||||||||||||||||||||||||||||||||
| 456 | 59697 | my $xs = floor($x/$side); | |||||||||||||||||||||||||||||||||||||||||
| 457 | 59697 | my $ys = floor($y/$side); | |||||||||||||||||||||||||||||||||||||||||
| 458 | 59697 | $x %= $side; | |||||||||||||||||||||||||||||||||||||||||
| 459 | 59697 | $y %= $side; | |||||||||||||||||||||||||||||||||||||||||
| 460 | 59697 | my $n; | |||||||||||||||||||||||||||||||||||||||||
| 461 | 59697 | ▶ | if ($xs > -$ys) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 462 | ### top or right | ||||||||||||||||||||||||||||||||||||||||||
| 463 | 28382 | ▶ | if ($xs >= $ys) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 464 | ### right going upwards | ||||||||||||||||||||||||||||||||||||||||||
| 465 | 16021 | $n = $squared*((4*$xs - 3)*$xs + $ys); | |||||||||||||||||||||||||||||||||||||||||
| 466 | 16021 | ($x,$y) = ($y,$turns-$x); # rotate -90 | |||||||||||||||||||||||||||||||||||||||||
| 467 | 16021 | ▶ | if ($x == 0) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 468 | 1777 | $x = $turns; | |||||||||||||||||||||||||||||||||||||||||
| 469 | 1777 | $n -= $side*$turns; # +$side modulo | |||||||||||||||||||||||||||||||||||||||||
| 470 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 471 | 14244 | $x -= 1; | |||||||||||||||||||||||||||||||||||||||||
| 472 | 14244 | $n += $side; | |||||||||||||||||||||||||||||||||||||||||
| 473 | } | ||||||||||||||||||||||||||||||||||||||||||
| 474 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 475 | ### top going leftwards | ||||||||||||||||||||||||||||||||||||||||||
| 476 | 12361 | $n = $squared*((4*$ys - 1)*$ys - $xs); | |||||||||||||||||||||||||||||||||||||||||
| 477 | 12361 | $x = $turns-$x; # rotate 180 | |||||||||||||||||||||||||||||||||||||||||
| 478 | 12361 | $y = $turns-$y; | |||||||||||||||||||||||||||||||||||||||||
| 479 | } | ||||||||||||||||||||||||||||||||||||||||||
| 480 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 481 | ### bottom or left | ||||||||||||||||||||||||||||||||||||||||||
| 482 | 31315 | ▶ | if ($xs > $ys || ($xs == 0 && $ys == 0)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition83%5/6
$xs > $ys or $xs == 0 and $ys == 0
$xs == 0 and $ys == 0
MC/DC66%2/3
$xs > $ys or $xs == 0 and $ys == 0
$xs > $ys
$xs == 0
$ys == 0
Truth table75%3/4
A or B and C
A= $xs > $ysB= $xs == 0C= $ys == 0
| |||||||||||||||||||||||||||||||||||||||||||
| 483 | ### bottom going rightwards: "$xs,$ys" | ||||||||||||||||||||||||||||||||||||||||||
| 484 | 13874 | $n = $squared*((4*$ys - 3)*$ys + $xs); | |||||||||||||||||||||||||||||||||||||||||
| 485 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 486 | ### left going downwards | ||||||||||||||||||||||||||||||||||||||||||
| 487 | 17441 | $n = $squared*((4*$xs - 1)*$xs - $ys); | |||||||||||||||||||||||||||||||||||||||||
| 488 | 17441 | ($x,$y) = ($turns-$y,$x); # rotate +90 | |||||||||||||||||||||||||||||||||||||||||
| 489 | 17441 | ▶ | if ($x == 0) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 490 | 1159 | $x = $turns; | |||||||||||||||||||||||||||||||||||||||||
| 491 | 1159 | $n -= $side*$turns; # +$side modulo | |||||||||||||||||||||||||||||||||||||||||
| 492 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 493 | 16282 | $x -= 1; | |||||||||||||||||||||||||||||||||||||||||
| 494 | 16282 | $n += $side; | |||||||||||||||||||||||||||||||||||||||||
| 495 | } | ||||||||||||||||||||||||||||||||||||||||||
| 496 | } | ||||||||||||||||||||||||||||||||||||||||||
| 497 | } | ||||||||||||||||||||||||||||||||||||||||||
| 498 | |||||||||||||||||||||||||||||||||||||||||||
| 499 | 59697 | ▶ | if ($x + $y >= $turns) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 500 | ### key top or right ... | ||||||||||||||||||||||||||||||||||||||||||
| 501 | 32516 | ▶ | if ($x > $y) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 502 | ### key right ... | ||||||||||||||||||||||||||||||||||||||||||
| 503 | 15113 | $x = $turns-$x; | |||||||||||||||||||||||||||||||||||||||||
| 504 | 15113 | ▶ | if ($x % 2) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 505 | ### forward ... | ||||||||||||||||||||||||||||||||||||||||||
| 506 | 6321 | $n += (2*$turns-2*$x+2)*$x + $y - $turns; | |||||||||||||||||||||||||||||||||||||||||
| 507 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 508 | ### backward ... | ||||||||||||||||||||||||||||||||||||||||||
| 509 | 8792 | $n += $squared - (2*$turns-2*$x+2)*$x - $y; | |||||||||||||||||||||||||||||||||||||||||
| 510 | } | ||||||||||||||||||||||||||||||||||||||||||
| 511 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 512 | ### key top ... | ||||||||||||||||||||||||||||||||||||||||||
| 513 | 17403 | $y = $turns-$y; | |||||||||||||||||||||||||||||||||||||||||
| 514 | 17403 | ▶ | if ($y % 2) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 515 | ### backward ... | ||||||||||||||||||||||||||||||||||||||||||
| 516 | 7192 | $n += (2*$turns-2*$y)*$y + $turns-$x; | |||||||||||||||||||||||||||||||||||||||||
| 517 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 518 | ### forward ... | ||||||||||||||||||||||||||||||||||||||||||
| 519 | 10211 | $n += $squared - (2*$turns - 2*$y)*$y - 2*$turns + $x; | |||||||||||||||||||||||||||||||||||||||||
| 520 | } | ||||||||||||||||||||||||||||||||||||||||||
| 521 | } | ||||||||||||||||||||||||||||||||||||||||||
| 522 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 523 | ### key bottom or left ... | ||||||||||||||||||||||||||||||||||||||||||
| 524 | 27181 | ▶ | if ($x >= $y) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 525 | ### key bottom ... | ||||||||||||||||||||||||||||||||||||||||||
| 526 | 14516 | ▶ | if ($y % 2) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 527 | ### backward ... | ||||||||||||||||||||||||||||||||||||||||||
| 528 | 6303 | $n += $squared - (2*$turns - 2*$y)*$y - $turns - $x - 1; | |||||||||||||||||||||||||||||||||||||||||
| 529 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 530 | ### forward ... | ||||||||||||||||||||||||||||||||||||||||||
| 531 | 8213 | $n += (2*$turns-2*$y)*$y + $x + 1; | |||||||||||||||||||||||||||||||||||||||||
| 532 | } | ||||||||||||||||||||||||||||||||||||||||||
| 533 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 534 | ### key left ... | ||||||||||||||||||||||||||||||||||||||||||
| 535 | 12665 | ▶ | if ($x % 2) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 536 | ### forward ... | ||||||||||||||||||||||||||||||||||||||||||
| 537 | 5480 | $n += (2*$turns-2*$x-2)*$x + 2*$turns - $y; | |||||||||||||||||||||||||||||||||||||||||
| 538 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 539 | ### backward ... | ||||||||||||||||||||||||||||||||||||||||||
| 540 | 7185 | $n += $squared - (2*$turns - 2*$x - 2)*$x - 3*$turns + $y; | |||||||||||||||||||||||||||||||||||||||||
| 541 | } | ||||||||||||||||||||||||||||||||||||||||||
| 542 | } | ||||||||||||||||||||||||||||||||||||||||||
| 543 | } | ||||||||||||||||||||||||||||||||||||||||||
| 544 | |||||||||||||||||||||||||||||||||||||||||||
| 545 | 59697 | return $n + $self->{'n_start'}-1; | |||||||||||||||||||||||||||||||||||||||||
| 546 | } | ||||||||||||||||||||||||||||||||||||||||||
| 547 | |||||||||||||||||||||||||||||||||||||||||||
| 548 | 1 | ▶ | use Math::PlanePath::SquareArms; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 549 | 1 | *_rect_square_range = \&Math::PlanePath::SquareArms::_rect_square_range; | |||||||||||||||||||||||||||||||||||||||||
| 550 | |||||||||||||||||||||||||||||||||||||||||||
| 551 | # not exact | ||||||||||||||||||||||||||||||||||||||||||
| 552 | sub rect_to_n_range { | ||||||||||||||||||||||||||||||||||||||||||
| 553 | 1400 | ▶ | my ($self, $x1,$y1, $x2,$y2) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
rect_to_n_range: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 554 | ### rect_to_n_range(): "$x1,$y1 $x2,$y2" | ||||||||||||||||||||||||||||||||||||||||||
| 555 | |||||||||||||||||||||||||||||||||||||||||||
| 556 | 1400 | $x1 = round_nearest ($x1); | |||||||||||||||||||||||||||||||||||||||||
| 557 | 1400 | $y1 = round_nearest ($y1); | |||||||||||||||||||||||||||||||||||||||||
| 558 | 1400 | $x2 = round_nearest ($x2); | |||||||||||||||||||||||||||||||||||||||||
| 559 | 1400 | $y2 = round_nearest ($y2); | |||||||||||||||||||||||||||||||||||||||||
| 560 | |||||||||||||||||||||||||||||||||||||||||||
| 561 | # floor divisions to square blocks | ||||||||||||||||||||||||||||||||||||||||||
| 562 | { | ||||||||||||||||||||||||||||||||||||||||||
| 563 | 1400 | my $side = $self->{'turns'} + 1; | |||||||||||||||||||||||||||||||||||||||||
| 564 | 1400 | _divrem_mutate($x1,$side); | |||||||||||||||||||||||||||||||||||||||||
| 565 | 1400 | _divrem_mutate($y1,$side); | |||||||||||||||||||||||||||||||||||||||||
| 566 | 1400 | _divrem_mutate($x2,$side); | |||||||||||||||||||||||||||||||||||||||||
| 567 | 1400 | _divrem_mutate($y2,$side); | |||||||||||||||||||||||||||||||||||||||||
| 568 | } | ||||||||||||||||||||||||||||||||||||||||||
| 569 | 1400 | my ($dlo, $dhi) = _rect_square_range ($x1, $y1, | |||||||||||||||||||||||||||||||||||||||||
| 570 | $x2, $y2); | ||||||||||||||||||||||||||||||||||||||||||
| 571 | 1400 | my $squared = $self->{'squared'}; | |||||||||||||||||||||||||||||||||||||||||
| 572 | |||||||||||||||||||||||||||||||||||||||||||
| 573 | ### d range sides: "$dlo, $dhi" | ||||||||||||||||||||||||||||||||||||||||||
| 574 | ### right start: ((4*$squared*$dlo - 4*$squared)*$dlo + 10) | ||||||||||||||||||||||||||||||||||||||||||
| 575 | |||||||||||||||||||||||||||||||||||||||||||
| 576 | return (($dlo == 0 ? 0 # special case Nlo=1 for innermost square | ||||||||||||||||||||||||||||||||||||||||||
| 577 | # Nlo at right vertical start | ||||||||||||||||||||||||||||||||||||||||||
| 578 | : ((4*$squared*$dlo - 4*$squared)*$dlo + $squared)) | ||||||||||||||||||||||||||||||||||||||||||
| 579 | + $self->{'n_start'}, | ||||||||||||||||||||||||||||||||||||||||||
| 580 | |||||||||||||||||||||||||||||||||||||||||||
| 581 | # Nhi at bottom horizontal end | ||||||||||||||||||||||||||||||||||||||||||
| 582 | (4*$squared*$dhi + 4*$squared)*$dhi | ||||||||||||||||||||||||||||||||||||||||||
| 583 | + $squared | ||||||||||||||||||||||||||||||||||||||||||
| 584 | 1400 | ▶ | + $self->{'n_start'}-1); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 585 | } | ||||||||||||||||||||||||||||||||||||||||||
| 586 | |||||||||||||||||||||||||||||||||||||||||||
| 587 | 1 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 588 | __END__ | ||||||||||||||||||||||||||||||||||||||||||