| 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 | |||||||||||||||||||||||||||||
| 20 | # math-image --path=GosperIslands --lines --scale=10 | ||||||||||||||||||||||||||||
| 21 | # math-image --path=GosperIslands --all --output=numbers_dash | ||||||||||||||||||||||||||||
| 22 | # | ||||||||||||||||||||||||||||
| 23 | # fractal dimension 2*log(3) / log(7) = 1.12915, A113211 decimal | ||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | # Check: | ||||||||||||||||||||||||||||
| 26 | # A017926 boundary length, unit equilateral triangles | ||||||||||||||||||||||||||||
| 27 | # A229977 boundary length, initial hexagon=1 | ||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | package Math::PlanePath::GosperIslands; | ||||||||||||||||||||||||||||
| 31 | 2 | ▶ | use 5.004; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 32 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 33 | 2 | ▶ | use POSIX 'ceil'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 34 | 2 | ▶ | use Math::Libm 'hypot'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 35 | #use List::Util 'max'; | ||||||||||||||||||||||||||||
| 36 | 2 | *max = \&Math::PlanePath::_max; | |||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | 2 | ▶ | use vars '$VERSION', '@ISA'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 39 | 2 | $VERSION = 128; | |||||||||||||||||||||||||||
| 40 | 2 | ▶ | use Math::PlanePath; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 41 | 2 | @ISA = ('Math::PlanePath'); | |||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | use Math::PlanePath::Base::Generic | ||||||||||||||||||||||||||||
| 44 | 2 | 'is_infinite', | |||||||||||||||||||||||||||
| 45 | 2 | ▶ | 'round_nearest'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 46 | use Math::PlanePath::Base::Digits | ||||||||||||||||||||||||||||
| 47 | 2 | 'round_down_pow', | |||||||||||||||||||||||||||
| 48 | 2 | ▶ | 'digit_split_lowtohigh'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | 2 | ▶ | use Math::PlanePath::SacksSpiral; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | # uncomment this to run the ### lines | ||||||||||||||||||||||||||||
| 53 | # use Smart::Comments; | ||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | 2 | ▶ | use constant n_frac_discontinuity => 0; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 57 | 2 | ▶ | use constant x_negative_at_n => 3; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 58 | 2 | ▶ | use constant y_negative_at_n => 5; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 59 | 2 | ▶ | use constant sumabsxy_minimum => 2; # minimum X=2,Y=0 or X=1,Y=1 | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 60 | 2 | ▶ | use constant rsquared_minimum => 2; # minimum X=1,Y=1 | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | # jump across rings is upwards, so dY maximum unbounded but minimum=-1 | ||||||||||||||||||||||||||||
| 63 | 2 | ▶ | use constant dy_minimum => -1; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | # dX and dY unbounded jumping between rings, with the jump position rotating | ||||||||||||||||||||||||||||
| 66 | # around slowly with the twistiness of the ring. | ||||||||||||||||||||||||||||
| 67 | 2 | ▶ | use constant absdx_minimum => 1; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | # jump across rings is ENE, so dSum maximum unbounded but minimum=-2 | ||||||||||||||||||||||||||||
| 70 | 2 | ▶ | use constant dsumxy_minimum => -2; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | # jump across rings is ENE, so dDiffXY minimum unbounded but maximum=+2 | ||||||||||||||||||||||||||||
| 73 | 2 | ▶ | use constant ddiffxy_maximum => 2; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | 2 | ▶ | use constant dir_maximum_dxdy => (1,-1); # South-East | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 76 | 2 | ▶ | use constant turn_any_straight => 0; # never straight | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||
| 80 | sub new { | ||||||||||||||||||||||||||||
| 81 | 5 | ▶ | my $self = shift->SUPER::new (@_); | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||||||||||||||||||||
| 82 | 5 | ▶ | $self->{'sides'} ||= 6; # default | ||||||||||||||||||||||||||
|
Condition50%1/2
$self->{"sides"} ||= 6
MC/DC0%0/1
$self->{"sides"} ||= 6
$self->{"sides"}
Truth table50%1/2
A
A= $self->{"sides"}
| |||||||||||||||||||||||||||||
| 83 | 5 | return $self; | |||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | # innermost hexagon level 0 | ||||||||||||||||||||||||||||
| 87 | # level 0 len=6 | ||||||||||||||||||||||||||||
| 88 | # level 1 len=18 | ||||||||||||||||||||||||||||
| 89 | # each sidelen = 3^level | ||||||||||||||||||||||||||||
| 90 | # each ringlen = 6*3^level | ||||||||||||||||||||||||||||
| 91 | # | ||||||||||||||||||||||||||||
| 92 | # Nstart(level) = 1 + ringlen(0) + ... + ringlen(level-1) | ||||||||||||||||||||||||||||
| 93 | # = 1 + 6* [ 3^0 + ... + 3^(level-1) ] | ||||||||||||||||||||||||||||
| 94 | # = 1 + 6* [ (3^level - 1) / 2 ] | ||||||||||||||||||||||||||||
| 95 | # = 1 + 3*(3^level - 1) | ||||||||||||||||||||||||||||
| 96 | # = 1 + 3*3^level - 3 | ||||||||||||||||||||||||||||
| 97 | # = 3^(level+1) - 2 | ||||||||||||||||||||||||||||
| 98 | # | ||||||||||||||||||||||||||||
| 99 | # 3^(level+1) = N+2 | ||||||||||||||||||||||||||||
| 100 | # level+1 = log3(N+2) | ||||||||||||||||||||||||||||
| 101 | # level = log3(N+2) - 1 | ||||||||||||||||||||||||||||
| 102 | # | ||||||||||||||||||||||||||||
| 103 | # sides=3 | ||||||||||||||||||||||||||||
| 104 | # ringlen = 3*3^level | ||||||||||||||||||||||||||||
| 105 | # Nstart(level) = 1 + 3* [ 3^0 + ... + 3^(level-1) ] | ||||||||||||||||||||||||||||
| 106 | # = 1 + 3* [ (3^level - 1) / 2 ] | ||||||||||||||||||||||||||||
| 107 | # = 1 + 3*(3^level - 1)/2 | ||||||||||||||||||||||||||||
| 108 | # = 1 + (3*3^level - 3)/2 | ||||||||||||||||||||||||||||
| 109 | # = (3*3^level - 3 + 2)/2 | ||||||||||||||||||||||||||||
| 110 | # = (3^(level+1) - 1)/2 | ||||||||||||||||||||||||||||
| 111 | # 3^(level+1) - 1 = 2*N | ||||||||||||||||||||||||||||
| 112 | # 3^(level+1) = 2*N+1 | ||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | 2 | my @_xend = (2, 5); | |||||||||||||||||||||||||||
| 115 | 2 | my @_yend = (0, 1); | |||||||||||||||||||||||||||
| 116 | 2 | my @_hypotend = (4, 28); | |||||||||||||||||||||||||||
| 117 | sub _ends_for_level { | ||||||||||||||||||||||||||||
| 118 | 236023 | ▶ | my ($level) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_ends_for_level: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 119 | 236023 | ▶ | if ($#_xend < $level) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 120 | 11 | my $x = $_xend[-1]; | |||||||||||||||||||||||||||
| 121 | 11 | my $y = $_yend[-1]; | |||||||||||||||||||||||||||
| 122 | 11 | ▶ | do { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 123 | 28 | ($x,$y) = ((5*$x - 3*$y)/2, # 2*$x + rotate +60 | |||||||||||||||||||||||||||
| 124 | ($x + 5*$y)/2); # 2*$y + rotate +60 | ||||||||||||||||||||||||||||
| 125 | ### _ends_for_level() push: scalar(@_xend)." $x,$y" | ||||||||||||||||||||||||||||
| 126 | # ### assert: "$x,$y" eq join(','__PACKAGE__->n_to_xy(scalar(@xend) ** 3)) | ||||||||||||||||||||||||||||
| 127 | 28 | push @_xend, $x; | |||||||||||||||||||||||||||
| 128 | 28 | push @_yend, $y; | |||||||||||||||||||||||||||
| 129 | 28 | push @_hypotend, $_hypotend[-1] * 7; | |||||||||||||||||||||||||||
| 130 | } while ($#_xend < $level); | ||||||||||||||||||||||||||||
| 131 | } | ||||||||||||||||||||||||||||
| 132 | } | ||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | 2 | my @level_x = (0); | |||||||||||||||||||||||||||
| 135 | 2 | my @level_y = (0); | |||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | sub n_to_xy { | ||||||||||||||||||||||||||||
| 138 | 517 | ▶ | my ($self, $n) = @_; | ||||||||||||||||||||||||||
|
Subroutine
n_to_xy: called CC=13 SCAR=35.8
Pod
documented
| |||||||||||||||||||||||||||||
| 139 | ### GosperIslands n_to_xy(): $n | ||||||||||||||||||||||||||||
| 140 | 517 | ▶ | if ($n < 1) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 141 | 0 | return; | |||||||||||||||||||||||||||
| 142 | } | ||||||||||||||||||||||||||||
| 143 | 517 | ▶ | if (is_infinite($n)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 144 | 0 | return ($n,$n); | |||||||||||||||||||||||||||
| 145 | } | ||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | 517 | my $sides = $self->{'sides'}; | |||||||||||||||||||||||||||
| 148 | 517 | ▶ | my ($pow, $level) = round_down_pow (($sides == 6 ? $n+2 : 2*$n+1), | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 149 | 3); | ||||||||||||||||||||||||||||
| 150 | 517 | ▶ | my $base = ($sides == 6 ? $pow-2 : ($pow-1)/2); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 151 | ### $level | ||||||||||||||||||||||||||||
| 152 | ### $base | ||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | 517 | $n -= $base; # remainder | |||||||||||||||||||||||||||
| 155 | 517 | my $sidelen = $pow / 3; | |||||||||||||||||||||||||||
| 156 | 517 | $level--; | |||||||||||||||||||||||||||
| 157 | 517 | my $side = int ($n / $sidelen); | |||||||||||||||||||||||||||
| 158 | 517 | my ($x, $y) = _side_n_to_xy ($n - $side*$sidelen); | |||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||
| 160 | 517 | _ends_for_level($level); | |||||||||||||||||||||||||||
| 161 | ### raw xy: "$x,$y" | ||||||||||||||||||||||||||||
| 162 | ### pos: "$_xend[$level],$_yend[$level]" | ||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||
| 164 | 517 | ▶ | if ($sides == 6) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 165 | 517 | ($x,$y) = (($x+3*$y)/-2, # rotate +120 | |||||||||||||||||||||||||||
| 166 | ($x-$y)/2); | ||||||||||||||||||||||||||||
| 167 | ### rotate xy: "$x,$y" | ||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||
| 169 | 517 | $x += $_xend[$level]; | |||||||||||||||||||||||||||
| 170 | 517 | $y += $_yend[$level]; | |||||||||||||||||||||||||||
| 171 | 517 | ▶ | if ($side >= 3) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 172 | 174 | $x = -$x; # rotate 180 | |||||||||||||||||||||||||||
| 173 | 174 | $y = -$y; | |||||||||||||||||||||||||||
| 174 | 174 | $side -= 3; | |||||||||||||||||||||||||||
| 175 | } | ||||||||||||||||||||||||||||
| 176 | 517 | ▶ | if ($side == 1) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 177 | 177 | ($x,$y) = (($x-3*$y)/2, # rotate +60 | |||||||||||||||||||||||||||
| 178 | ($x+$y)/2); | ||||||||||||||||||||||||||||
| 179 | } elsif ($side == 2) { | ||||||||||||||||||||||||||||
| 180 | 124 | ($x,$y) = (($x+3*$y)/-2, # rotate +120 | |||||||||||||||||||||||||||
| 181 | ($x-$y)/2); | ||||||||||||||||||||||||||||
| 182 | } | ||||||||||||||||||||||||||||
| 183 | } else { | ||||||||||||||||||||||||||||
| 184 | 0 | my $xend = $_xend[$level]; | |||||||||||||||||||||||||||
| 185 | 0 | my $yend = $_yend[$level]; | |||||||||||||||||||||||||||
| 186 | 0 | my $xp = 0; | |||||||||||||||||||||||||||
| 187 | 0 | my $yp = 0; | |||||||||||||||||||||||||||
| 188 | 0 | foreach (0 .. $level-1) { | |||||||||||||||||||||||||||
| 189 | 0 | $xp +=$_xend[$_]; | |||||||||||||||||||||||||||
| 190 | 0 | $yp +=$_yend[$_]; | |||||||||||||||||||||||||||
| 191 | } | ||||||||||||||||||||||||||||
| 192 | ### ends: "$xend,$yend prev $xp,$yp" | ||||||||||||||||||||||||||||
| 193 | 0 | ($xp,$yp) = (($xp-3*$yp)/-2, # rotate -120 | |||||||||||||||||||||||||||
| 194 | ($xp+$yp)/-2); | ||||||||||||||||||||||||||||
| 195 | # ($xp,$yp) = (($xp-3*$yp)/2, # rotate +60 | ||||||||||||||||||||||||||||
| 196 | # ($xp+$yp)/2); | ||||||||||||||||||||||||||||
| 197 | 0 | ▶ | if ($side == 0) { | ||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||
| 198 | 0 | $x += $xp; | |||||||||||||||||||||||||||
| 199 | 0 | $y += $yp; | |||||||||||||||||||||||||||
| 200 | } elsif ($side == 1) { | ||||||||||||||||||||||||||||
| 201 | 0 | ($x,$y) = (($x+3*$y)/-2, # rotate +120 | |||||||||||||||||||||||||||
| 202 | ($x-$y)/2); | ||||||||||||||||||||||||||||
| 203 | 0 | $x += $xp; | |||||||||||||||||||||||||||
| 204 | 0 | $y += $yp; | |||||||||||||||||||||||||||
| 205 | 0 | $x += $xend; | |||||||||||||||||||||||||||
| 206 | 0 | $y += $yend; | |||||||||||||||||||||||||||
| 207 | } elsif ($side == 2) { | ||||||||||||||||||||||||||||
| 208 | 0 | ($x,$y) = (($x-3*$y)/-2, # rotate +240==-120 | |||||||||||||||||||||||||||
| 209 | ($x+$y)/-2); | ||||||||||||||||||||||||||||
| 210 | 0 | $x += $xp; | |||||||||||||||||||||||||||
| 211 | 0 | $y += $yp; | |||||||||||||||||||||||||||
| 212 | 0 | $x += $xend; | |||||||||||||||||||||||||||
| 213 | 0 | $y += $yend; | |||||||||||||||||||||||||||
| 214 | 0 | ($xend,$yend) = (($xend+3*$yend)/-2, # rotate +120 | |||||||||||||||||||||||||||
| 215 | ($xend-$yend)/2); | ||||||||||||||||||||||||||||
| 216 | 0 | $x += $xend; | |||||||||||||||||||||||||||
| 217 | 0 | $y += $yend; | |||||||||||||||||||||||||||
| 218 | } | ||||||||||||||||||||||||||||
| 219 | } | ||||||||||||||||||||||||||||
| 220 | 517 | return ($x,$y); | |||||||||||||||||||||||||||
| 221 | } | ||||||||||||||||||||||||||||
| 222 | |||||||||||||||||||||||||||||
| 223 | # ENHANCE-ME: share with GosperSide ? | ||||||||||||||||||||||||||||
| 224 | sub _side_n_to_xy { | ||||||||||||||||||||||||||||
| 225 | 517 | ▶ | my ($n) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_side_n_to_xy: called CC=6 SCAR=18.0
| |||||||||||||||||||||||||||||
| 226 | ### _side_n_to_xy(): $n | ||||||||||||||||||||||||||||
| 227 | 517 | ▶ | if ($n < 0) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 228 | 0 | return; | |||||||||||||||||||||||||||
| 229 | } | ||||||||||||||||||||||||||||
| 230 | 517 | ▶ | if (is_infinite($n)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 231 | 0 | return ($n,$n); | |||||||||||||||||||||||||||
| 232 | } | ||||||||||||||||||||||||||||
| 233 | |||||||||||||||||||||||||||||
| 234 | 517 | my $x; | |||||||||||||||||||||||||||
| 235 | 517 | my $y = 0; | |||||||||||||||||||||||||||
| 236 | { | ||||||||||||||||||||||||||||
| 237 | 517 | my $int = int($n); | |||||||||||||||||||||||||||
| 238 | 517 | $x = 2*($n - $int); | |||||||||||||||||||||||||||
| 239 | 517 | $n = $int; # BigFloat int() gives BigInt, use that | |||||||||||||||||||||||||||
| 240 | } | ||||||||||||||||||||||||||||
| 241 | 517 | my $xend = 2; | |||||||||||||||||||||||||||
| 242 | 517 | my $yend = 0; | |||||||||||||||||||||||||||
| 243 | |||||||||||||||||||||||||||||
| 244 | 517 | foreach my $digit (digit_split_lowtohigh($n,3)) { | |||||||||||||||||||||||||||
| 245 | 2373 | my $xend_offset = 3*($xend-$yend)/2; # end and end +60 | |||||||||||||||||||||||||||
| 246 | 2373 | my $yend_offset = ($xend+3*$yend)/2; | |||||||||||||||||||||||||||
| 247 | |||||||||||||||||||||||||||||
| 248 | ### at: "$x,$y" | ||||||||||||||||||||||||||||
| 249 | ### $digit | ||||||||||||||||||||||||||||
| 250 | ### $xend | ||||||||||||||||||||||||||||
| 251 | ### $yend | ||||||||||||||||||||||||||||
| 252 | ### $xend_offset | ||||||||||||||||||||||||||||
| 253 | ### $yend_offset | ||||||||||||||||||||||||||||
| 254 | |||||||||||||||||||||||||||||
| 255 | 2373 | ▶ | if ($digit == 1) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 256 | 851 | ($x,$y) = (($x-3*$y)/2 + $xend, # rotate +60 | |||||||||||||||||||||||||||
| 257 | ($x+$y)/2 + $yend); | ||||||||||||||||||||||||||||
| 258 | } elsif ($digit == 2) { | ||||||||||||||||||||||||||||
| 259 | 833 | $x += $xend_offset; # offset and offset +60 | |||||||||||||||||||||||||||
| 260 | 833 | $y += $yend_offset; | |||||||||||||||||||||||||||
| 261 | } | ||||||||||||||||||||||||||||
| 262 | 2373 | $xend += $xend_offset; # offset and offset +60 | |||||||||||||||||||||||||||
| 263 | 2373 | $yend += $yend_offset; | |||||||||||||||||||||||||||
| 264 | } | ||||||||||||||||||||||||||||
| 265 | |||||||||||||||||||||||||||||
| 266 | ### final: "$x,$y" | ||||||||||||||||||||||||||||
| 267 | 517 | return ($x, $y); | |||||||||||||||||||||||||||
| 268 | } | ||||||||||||||||||||||||||||
| 269 | |||||||||||||||||||||||||||||
| 270 | |||||||||||||||||||||||||||||
| 271 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||
| 272 | # xy_to_n() | ||||||||||||||||||||||||||||
| 273 | |||||||||||||||||||||||||||||
| 274 | # innermost origin 0,0 N=0 level 0, | ||||||||||||||||||||||||||||
| 275 | # first hexagon level 1 | ||||||||||||||||||||||||||||
| 276 | # | ||||||||||||||||||||||||||||
| 277 | # Nstart(level) = 3^level - 2 | ||||||||||||||||||||||||||||
| 278 | # sidelength(level) = 3^(level-1) | ||||||||||||||||||||||||||||
| 279 | # so Nstart(level) + side * sidelength(level) | ||||||||||||||||||||||||||||
| 280 | # = 3^level - 2 + side * 3^(level-1) | ||||||||||||||||||||||||||||
| 281 | # = (3 + side) * 3^(level-1) - 2 | ||||||||||||||||||||||||||||
| 282 | # | ||||||||||||||||||||||||||||
| 283 | sub xy_to_n { | ||||||||||||||||||||||||||||
| 284 | 5618 | ▶ | my ($self, $x_full, $y_full) = @_; | ||||||||||||||||||||||||||
|
Subroutine
xy_to_n: called CC=8 SCAR=21.2
Pod
documented
| |||||||||||||||||||||||||||||
| 285 | 5618 | $x_full = round_nearest($x_full); | |||||||||||||||||||||||||||
| 286 | 5618 | $y_full = round_nearest($y_full); | |||||||||||||||||||||||||||
| 287 | ### GosperIslands xy_to_n(): "$x_full,$y_full" | ||||||||||||||||||||||||||||
| 288 | |||||||||||||||||||||||||||||
| 289 | 5618 | foreach my $overflow (3*$x_full+3*$y_full, 3*$x_full-3*$y_full) { | |||||||||||||||||||||||||||
| 290 | 11236 | ▶ | if (is_infinite($overflow)) { return $overflow; } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 291 | } | ||||||||||||||||||||||||||||
| 292 | 5618 | ▶ | if (($x_full + $y_full) % 2) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 293 | ### odd point, not reached ... | ||||||||||||||||||||||||||||
| 294 | 0 | return undef; | |||||||||||||||||||||||||||
| 295 | } | ||||||||||||||||||||||||||||
| 296 | |||||||||||||||||||||||||||||
| 297 | 5618 | my $r = hypot($x_full,$y_full); | |||||||||||||||||||||||||||
| 298 | 5618 | my $level_limit = ceil(log($r+1)/log(sqrt(7))); | |||||||||||||||||||||||||||
| 299 | ### $r | ||||||||||||||||||||||||||||
| 300 | ### $level_limit | ||||||||||||||||||||||||||||
| 301 | 5618 | ▶ | if (is_infinite($level_limit)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 302 | 0 | return $level_limit; | |||||||||||||||||||||||||||
| 303 | } | ||||||||||||||||||||||||||||
| 304 | |||||||||||||||||||||||||||||
| 305 | 5618 | for my $level (0 .. $level_limit + 1) { | |||||||||||||||||||||||||||
| 306 | 33932 | _ends_for_level($level); | |||||||||||||||||||||||||||
| 307 | 33932 | my $xend = $_xend[$level]; | |||||||||||||||||||||||||||
| 308 | 33932 | my $yend = $_yend[$level]; | |||||||||||||||||||||||||||
| 309 | |||||||||||||||||||||||||||||
| 310 | 33932 | my $x = $x_full - $xend; | |||||||||||||||||||||||||||
| 311 | 33932 | my $y = $y_full - $yend; | |||||||||||||||||||||||||||
| 312 | ### level end: "$xend,$yend" | ||||||||||||||||||||||||||||
| 313 | ### level subtract to: "$x,$y" | ||||||||||||||||||||||||||||
| 314 | 33932 | ($x,$y) = ((3*$y-$x)/2, # rotate -120; | |||||||||||||||||||||||||||
| 315 | ($x+$y)/-2); | ||||||||||||||||||||||||||||
| 316 | ### level rotate to: "$x,$y" | ||||||||||||||||||||||||||||
| 317 | |||||||||||||||||||||||||||||
| 318 | 33932 | foreach my $side (0 .. 5) { | |||||||||||||||||||||||||||
| 319 | ### try: "level=$level side=$side $x,$y" | ||||||||||||||||||||||||||||
| 320 | 201064 | ▶ | if (defined (my $n = _xy_to_n_in_level($x,$y,$level))) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 321 | 873 | return ($side+3)*3**$level + $n - 2; | |||||||||||||||||||||||||||
| 322 | } | ||||||||||||||||||||||||||||
| 323 | 200191 | $x -= $xend; | |||||||||||||||||||||||||||
| 324 | 200191 | $y -= $yend; | |||||||||||||||||||||||||||
| 325 | ### subtract to: "$x,$y" | ||||||||||||||||||||||||||||
| 326 | 200191 | ($x,$y) = (($x+3*$y)/2, # rotate -60 | |||||||||||||||||||||||||||
| 327 | ($y-$x)/2); | ||||||||||||||||||||||||||||
| 328 | |||||||||||||||||||||||||||||
| 329 | } | ||||||||||||||||||||||||||||
| 330 | } | ||||||||||||||||||||||||||||
| 331 | 4745 | return undef; | |||||||||||||||||||||||||||
| 332 | } | ||||||||||||||||||||||||||||
| 333 | |||||||||||||||||||||||||||||
| 334 | sub _xy_to_n_in_level { | ||||||||||||||||||||||||||||
| 335 | 201574 | ▶ | my ($x, $y, $level) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_xy_to_n_in_level: called CC=6 SCAR=17.9
| |||||||||||||||||||||||||||||
| 336 | |||||||||||||||||||||||||||||
| 337 | 201574 | _ends_for_level($level); | |||||||||||||||||||||||||||
| 338 | 201574 | my @pending_n = (0); | |||||||||||||||||||||||||||
| 339 | 201574 | my @pending_x = ($x); | |||||||||||||||||||||||||||
| 340 | 201574 | my @pending_y = ($y); | |||||||||||||||||||||||||||
| 341 | 201574 | my @pending_level = ($level); | |||||||||||||||||||||||||||
| 342 | |||||||||||||||||||||||||||||
| 343 | 201574 | ▶ | while (@pending_n) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 344 | 400171 | my $n = pop @pending_n; | |||||||||||||||||||||||||||
| 345 | 400171 | $x = pop @pending_x; | |||||||||||||||||||||||||||
| 346 | 400171 | $y = pop @pending_y; | |||||||||||||||||||||||||||
| 347 | 400171 | $level = pop @pending_level; | |||||||||||||||||||||||||||
| 348 | ### consider: "$x,$y n=$n level=$level" | ||||||||||||||||||||||||||||
| 349 | |||||||||||||||||||||||||||||
| 350 | 400171 | ▶ | if ($level == 0) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 351 | 47824 | ▶ | if ($x == 0 && $y == 0) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$x == 0 and $y == 0
MC/DC100%2/2
$x == 0 and $y == 0
$x == 0
$y == 0
Truth table100%3/3
A and B
A= $x == 0B= $y == 0
| |||||||||||||||||||||||||||||
| 352 | 1383 | return $n; | |||||||||||||||||||||||||||
| 353 | } | ||||||||||||||||||||||||||||
| 354 | ### level=0 and not 0,0 | ||||||||||||||||||||||||||||
| 355 | 46441 | next; | |||||||||||||||||||||||||||
| 356 | } | ||||||||||||||||||||||||||||
| 357 | 352347 | ▶ | if (($x*$x+3*$y*$y) > $_hypotend[$level] * 1.1) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 358 | ### radius out of range: ($x*$x+3*$y*$y)." cf end ".$_hypotend[$level] | ||||||||||||||||||||||||||||
| 359 | 283684 | next; | |||||||||||||||||||||||||||
| 360 | } | ||||||||||||||||||||||||||||
| 361 | |||||||||||||||||||||||||||||
| 362 | 68663 | $level--; | |||||||||||||||||||||||||||
| 363 | 68663 | $n *= 3; | |||||||||||||||||||||||||||
| 364 | 68663 | my $xend = $_xend[$level]; | |||||||||||||||||||||||||||
| 365 | 68663 | my $yend = $_yend[$level]; | |||||||||||||||||||||||||||
| 366 | ### descend: "end=$xend,$yend on level=$level" | ||||||||||||||||||||||||||||
| 367 | |||||||||||||||||||||||||||||
| 368 | # digit 0 | ||||||||||||||||||||||||||||
| 369 | 68663 | push @pending_n, $n; | |||||||||||||||||||||||||||
| 370 | 68663 | push @pending_x, $x; | |||||||||||||||||||||||||||
| 371 | 68663 | push @pending_y, $y; | |||||||||||||||||||||||||||
| 372 | 68663 | push @pending_level, $level; | |||||||||||||||||||||||||||
| 373 | ### push: "$x,$y digit=0" | ||||||||||||||||||||||||||||
| 374 | |||||||||||||||||||||||||||||
| 375 | # digit 1 | ||||||||||||||||||||||||||||
| 376 | 68663 | $x -= $xend; | |||||||||||||||||||||||||||
| 377 | 68663 | $y -= $yend; | |||||||||||||||||||||||||||
| 378 | 68663 | ($x,$y) = (($x+3*$y)/2, # rotate -60 | |||||||||||||||||||||||||||
| 379 | ($y-$x)/2); | ||||||||||||||||||||||||||||
| 380 | 68663 | push @pending_n, $n + 1; | |||||||||||||||||||||||||||
| 381 | 68663 | push @pending_x, $x; | |||||||||||||||||||||||||||
| 382 | 68663 | push @pending_y, $y; | |||||||||||||||||||||||||||
| 383 | 68663 | push @pending_level, $level; | |||||||||||||||||||||||||||
| 384 | ### push: "$x,$y digit=1" | ||||||||||||||||||||||||||||
| 385 | |||||||||||||||||||||||||||||
| 386 | # digit 2 | ||||||||||||||||||||||||||||
| 387 | 68663 | $x -= $xend; | |||||||||||||||||||||||||||
| 388 | 68663 | $y -= $yend; | |||||||||||||||||||||||||||
| 389 | 68663 | ($x,$y) = (($x-3*$y)/2, # rotate +60 | |||||||||||||||||||||||||||
| 390 | ($x+$y)/2); | ||||||||||||||||||||||||||||
| 391 | 68663 | push @pending_n, $n + 2; | |||||||||||||||||||||||||||
| 392 | 68663 | push @pending_x, $x; | |||||||||||||||||||||||||||
| 393 | 68663 | push @pending_y, $y; | |||||||||||||||||||||||||||
| 394 | 68663 | push @pending_level, $level; | |||||||||||||||||||||||||||
| 395 | ### push: "$x,$y digit=2" | ||||||||||||||||||||||||||||
| 396 | } | ||||||||||||||||||||||||||||
| 397 | |||||||||||||||||||||||||||||
| 398 | 200191 | return undef; | |||||||||||||||||||||||||||
| 399 | } | ||||||||||||||||||||||||||||
| 400 | |||||||||||||||||||||||||||||
| 401 | # Each | ||||||||||||||||||||||||||||
| 402 | # *--- | ||||||||||||||||||||||||||||
| 403 | # / | ||||||||||||||||||||||||||||
| 404 | # ---* | ||||||||||||||||||||||||||||
| 405 | # is width=5 heightflat=1 is | ||||||||||||||||||||||||||||
| 406 | # hypot^2 = 5*5 + 3 * 1*1 | ||||||||||||||||||||||||||||
| 407 | # = 25+3 | ||||||||||||||||||||||||||||
| 408 | # = 28 | ||||||||||||||||||||||||||||
| 409 | # hypot = 2*sqrt(7) | ||||||||||||||||||||||||||||
| 410 | # | ||||||||||||||||||||||||||||
| 411 | # comes in closer to | ||||||||||||||||||||||||||||
| 412 | # level=1 n=9,x=2,y=2 is hypot=sqrt(2*2+3*2*2) = sqrt(16) = 4 | ||||||||||||||||||||||||||||
| 413 | # level=2 n=31,x=2,y=6 is hypot=sqrt(2*2+3*6*6) = sqrt(112) = sqrt(7)*4 | ||||||||||||||||||||||||||||
| 414 | # so | ||||||||||||||||||||||||||||
| 415 | # radius = 4 * sqrt(7)^(level-1) | ||||||||||||||||||||||||||||
| 416 | # radius/4 = sqrt(7)^(level-1) | ||||||||||||||||||||||||||||
| 417 | # level-1 = log(radius/4) / log(sqrt(7)) | ||||||||||||||||||||||||||||
| 418 | # level = log(radius/4) / log(sqrt(7)) + 1 | ||||||||||||||||||||||||||||
| 419 | # | ||||||||||||||||||||||||||||
| 420 | # Or | ||||||||||||||||||||||||||||
| 421 | # level=1 n=9,x=2,y=2 is h=2*2+3*2*2 = 16 | ||||||||||||||||||||||||||||
| 422 | # level=2 n=31,x=2,y=6 is h=2*2+3*6*6 = 112 = 7*16 | ||||||||||||||||||||||||||||
| 423 | # h = 16 * 7^(level-1) | ||||||||||||||||||||||||||||
| 424 | # h/16 = 7^(level-1) | ||||||||||||||||||||||||||||
| 425 | # level-1 = log(h/16) / log(7) | ||||||||||||||||||||||||||||
| 426 | # level = log(h/16) / log(7) + 1 | ||||||||||||||||||||||||||||
| 427 | # | ||||||||||||||||||||||||||||
| 428 | # is the next outer level, so high covering is the end of the previous | ||||||||||||||||||||||||||||
| 429 | # level, | ||||||||||||||||||||||||||||
| 430 | # | ||||||||||||||||||||||||||||
| 431 | # Nstart(level) - 1 = 3^(level+2) - 2 - 1 | ||||||||||||||||||||||||||||
| 432 | # = 3^(level+2) - 3 | ||||||||||||||||||||||||||||
| 433 | # | ||||||||||||||||||||||||||||
| 434 | # not exact | ||||||||||||||||||||||||||||
| 435 | sub rect_to_n_range { | ||||||||||||||||||||||||||||
| 436 | 18 | ▶ | my ($self, $x1,$y1, $x2,$y2) = @_; | ||||||||||||||||||||||||||
|
Subroutine
rect_to_n_range: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 437 | 18 | $y1 *= sqrt(3); | |||||||||||||||||||||||||||
| 438 | 18 | $y2 *= sqrt(3); | |||||||||||||||||||||||||||
| 439 | 18 | my ($r_lo, $r_hi) = Math::PlanePath::SacksSpiral::_rect_to_radius_range | |||||||||||||||||||||||||||
| 440 | ($x1,$y1, $x2,$y2); | ||||||||||||||||||||||||||||
| 441 | 18 | $r_hi *= 2; | |||||||||||||||||||||||||||
| 442 | 18 | my $level_plus_1 = ceil( log(max(1,$r_hi/4)) / log(sqrt(7)) ) + 2; | |||||||||||||||||||||||||||
| 443 | 18 | return (1, 3**$level_plus_1 - 3); | |||||||||||||||||||||||||||
| 444 | } | ||||||||||||||||||||||||||||
| 445 | |||||||||||||||||||||||||||||
| 446 | 2 | 1; | |||||||||||||||||||||||||||
| 447 | __END__ | ||||||||||||||||||||||||||||