| 1 | # Copyright 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=CretanLabyrinth --output=numbers_dash | ||
| 20 | # http://labyrinthlocator.com/labyrinth-typology/4341-classical-labyrinths | ||
| 21 | |||
| 22 | |||
| 23 | |||
| 24 | package Math::PlanePath::CretanLabyrinth; | ||
| 25 | use 5.004; | ||
| 26 | use strict; | ||
| 27 | #use List::Util 'max'; | ||
| 28 | *max = \&Math::PlanePath::_max; | ||
| 29 | |||
| 30 | use vars '$VERSION', '@ISA'; | ||
| 31 | $VERSION = 128; | ||
| 32 | use Math::PlanePath; | ||
| 33 | use Math::PlanePath::Base::NSEW; | ||
| 34 | @ISA = ('Math::PlanePath::Base::NSEW', | ||
| 35 | 'Math::PlanePath'); | ||
| 36 | |||
| 37 | use Math::PlanePath::Base::Generic | ||
| 38 | 'is_infinite', | ||
| 39 | 'round_nearest'; | ||
| 40 | *_sqrtint = \&Math::PlanePath::_sqrtint; | ||
| 41 | |||
| 42 | # uncomment this to run the ### lines | ||
| 43 | #use Smart::Comments; | ||
| 44 | |||
| 45 | |||
| 46 | use constant xy_is_visited => 1; | ||
| 47 | use constant x_negative_at_n => 7; | ||
| 48 | use constant y_negative_at_n => 13; | ||
| 49 | |||
| 50 | |||
| 51 | #------------------------------------------------------------------------------ | ||
| 52 | |||
| 53 | # 81-80-79 78 77 76 75 74 73 72 71 70 69 | ||
| 54 | # | | ||
| 55 | # 82 x--x-- x--x 68 | ||
| 56 | # | | | | ||
| 57 | # x x--x- x--x x 67 | ||
| 58 | # | | | | | ||
| 59 | # x 49-50-51-52-53-54-55 x 66 | ||
| 60 | # | | | | ||
| 61 | # 48 9--8--7--6--5 56 65 | ||
| 62 | # | | | | ||
| 63 | # 47 10 25-26-27 4 57 64 | ||
| 64 | # | | | | | | | | | | | ||
| 65 | # 46 11 24 29-28 3 58 x--x 63 x x--x x | ||
| 66 | # | | | | | | | | | ||
| 67 | # 45 12 23 30 1--2 59-60-61-62 x--x--x--x | ||
| 68 | # | | | | ||
| 69 | # 44 13 22 31-32-33 x--x--x--x x--x--x--x | ||
| 70 | # | | | | | | | | ||
| 71 | # 43 14 21-20-19 34 x x--x x | ||
| 72 | # | | | | | | | | ||
| 73 | # 42 15-16-17-18 35 | ||
| 74 | # | | ||
| 75 | # 41 40 39 38-37-36 | ||
| 76 | # | ||
| 77 | my @initial_n = (1,2, 5, 9,15,18,19,21,25,27,28,29,31,33,36,41,49,55,59); | ||
| 78 | my @initial_dx = (1,0,-1, 0, 1, 0,-1, 0, 1, 0,-1, 0, 1, 0,-1, 0, 1, 0, 1); | ||
| 79 | my @initial_dy = (0,1, 0,-1, 0, 1, 0, 1, 0,-1, 0,-1, 0,-1, 0, 1, 0,-1, 0); | ||
| 80 | my @initial_x = (0); | ||
| 81 | my @initial_y = (0); | ||
| 82 | { | ||
| 83 | my $x = 0; | ||
| 84 | my $y = 0; | ||
| 85 | foreach my $i (1 .. $#initial_n) { | ||
| 86 | my $len = $initial_n[$i] - $initial_n[$i-1]; | ||
| 87 | $x += $initial_dx[$i-1] * $len; | ||
| 88 | $y += $initial_dy[$i-1] * $len; | ||
| 89 | $initial_x[$i] = $x; | ||
| 90 | $initial_y[$i] = $y; | ||
| 91 | } | ||
| 92 | } | ||
| 93 | ### @initial_x | ||
| 94 | ### @initial_y | ||
| 95 | |||
| 96 | my @len = ( 4,3,7,12,14,11,5, 1, 4, 9,12,10, 5, 1,4, 8,10,7,4,3, 7,13,16,14, 8); | ||
| 97 | my @dlen = ( 1,0,1, 2, 2, 2,1, 0, 1, 2, 2, 2, 1, 0,1, 2, 2,2,1,0, 1, 2, 2, 2, 1); | ||
| 98 | my @dx = ( 0,1,0,-1, 0, 1,0,-1, 0,-1, 0, 1, 0,-1,0,-1, 0,1,0,1, 0,-1, 0, 1, 0); | ||
| 99 | my @dy = (-1,0,1, 0,-1, 0,1, 0,-1, 0, 1, 0,-1, 0,1, 0,-1,0,1,0,-1, 0, 1, 0,-1); | ||
| 100 | |||
| 101 | # [0,1,2],[59, 247, 563,] | ||
| 102 | # N = (64 d^2 + 124 d + 59) | ||
| 103 | # = ((64*$d + 124)*$d + 59) | ||
| 104 | # d = -31/32 + sqrt(1/64 * $n + 17/1024) | ||
| 105 | # = (-31 + 32*sqrt(1/64 * $n + 17/1024)) / 32 | ||
| 106 | # = (-31 + sqrt(32*32/64 * $n + 32*32*17/1024)) / 32 | ||
| 107 | # = (-31 + sqrt(16*$n + 17)) / 32 | ||
| 108 | |||
| 109 | # [0,1,2],[55, 239, 551,] | ||
| 110 | # N = (64 d^2 + 120 d + 55) | ||
| 111 | # = ((64*$d + 120)*$d + 55) | ||
| 112 | # d = -15/16 + sqrt(1/64 * $n + 5/256) | ||
| 113 | # = (-15 + sqrt(16*16/64 * $n + 16*16*5/256))/16 | ||
| 114 | # = ((-15 + sqrt(4*$n + 5))/16) | ||
| 115 | |||
| 116 | |||
| 117 | sub n_to_xy { | ||
| 118 | my ($self, $n) = @_; | ||
| 119 | ### CretanLabyrinth n_to_xy(): $n | ||
| 120 | |||
| 121 | if ($n < 1) { return; } | ||
| 122 | if (is_infinite($n)) { return ($n, $n); } | ||
| 123 | |||
| 124 | if ($n < 55) { | ||
| 125 | foreach my $i (0 .. $#initial_n-1) { | ||
| 126 | if ($initial_n[$i+1] > $n) { | ||
| 127 | $n -= $initial_n[$i]; | ||
| 128 | ### $n | ||
| 129 | return ($initial_x[$i] + $initial_dx[$i] * $n, | ||
| 130 | $initial_y[$i] + $initial_dy[$i] * $n); | ||
| 131 | } | ||
| 132 | } | ||
| 133 | } | ||
| 134 | |||
| 135 | my $d = int( (-15 + _sqrtint(4*$n+5))/16 ); | ||
| 136 | $n -= ((64*$d + 120)*$d + 55); | ||
| 137 | |||
| 138 | my $x = 4*$d + 2; | ||
| 139 | my $y = 4*$d + 4; | ||
| 140 | |||
| 141 | ### $d | ||
| 142 | ### $n | ||
| 143 | ### $x | ||
| 144 | ### $y | ||
| 145 | |||
| 146 | foreach my $i (0 .. $#len) { | ||
| 147 | my $len = $len[$i] + 4*$d*$dlen[$i]; | ||
| 148 | if ($n <= $len) { | ||
| 149 | ### $i | ||
| 150 | return ($n*$dx[$i] + $x, # $n first to inherit BigRat | ||
| 151 | $n*$dy[$i] + $y); | ||
| 152 | } | ||
| 153 | $n -= $len; | ||
| 154 | $x += $dx[$i]*$len; | ||
| 155 | $y += $dy[$i]*$len; | ||
| 156 | } | ||
| 157 | die "oops, end of lengths table reached"; | ||
| 158 | } | ||
| 159 | |||
| 160 | sub xy_to_n { | ||
| 161 | my ($self, $x, $y) = @_; | ||
| 162 | ### CretanLabyrinth xy_to_n(): "$x, $y" | ||
| 163 | |||
| 164 | $x = round_nearest($x); | ||
| 165 | $y = round_nearest($y); | ||
| 166 | my $d = _xy_to_d($x,$y); | ||
| 167 | ### $d | ||
| 168 | |||
| 169 | if ($d < 1) { | ||
| 170 | foreach my $i (0 .. $#initial_n-1) { | ||
| 171 | my $len = $initial_n[$i+1] - $initial_n[$i]; | ||
| 172 | my $rx = $x - $initial_x[$i]; | ||
| 173 | my $ry = $y - $initial_y[$i]; | ||
| 174 | |||
| 175 | if ($initial_dx[$i]) { | ||
| 176 | $rx *= $initial_dx[$i]; | ||
| 177 | } else { | ||
| 178 | next if $rx; | ||
| 179 | } | ||
| 180 | |||
| 181 | if ($initial_dy[$i]) { | ||
| 182 | $ry *= $initial_dy[$i]; | ||
| 183 | } else { | ||
| 184 | next if $ry; | ||
| 185 | } | ||
| 186 | |||
| 187 | if ($rx >= 0 && $rx <= $len && $ry >= 0 && $ry <= $len) { | ||
| 188 | return $initial_n[$i] + $rx + $ry; | ||
| 189 | } | ||
| 190 | } | ||
| 191 | } else { | ||
| 192 | $d -= 1; | ||
| 193 | ### $d | ||
| 194 | my $tx = 4*$d + 2; | ||
| 195 | my $ty = 4*$d + 4; | ||
| 196 | my $n = ((64*$d + 120)*$d + 55); | ||
| 197 | foreach my $i (0 .. $#len) { | ||
| 198 | ### at: "txy=$tx,$ty n=$n" | ||
| 199 | |||
| 200 | my $len = $len[$i] + 4*$d*$dlen[$i]; | ||
| 201 | |||
| 202 | my $rx = $x - $tx; | ||
| 203 | my $ry = $y - $ty; | ||
| 204 | $tx += $dx[$i]*$len; | ||
| 205 | $ty += $dy[$i]*$len; | ||
| 206 | $n += $len; | ||
| 207 | ### rxy: "$rx,$ry" | ||
| 208 | |||
| 209 | if ($dx[$i]) { | ||
| 210 | $rx *= $dx[$i]; | ||
| 211 | } else { | ||
| 212 | next if $rx; | ||
| 213 | } | ||
| 214 | |||
| 215 | if ($dy[$i]) { | ||
| 216 | $ry *= $dy[$i]; | ||
| 217 | } else { | ||
| 218 | next if $ry; | ||
| 219 | } | ||
| 220 | |||
| 221 | if ($rx >= 0 && $rx <= $len && $ry >= 0 && $ry <= $len) { | ||
| 222 | ### found: "n=".($n-$len)." plus ".($rx+$ry) | ||
| 223 | return $n-$len + $rx + $ry; | ||
| 224 | } | ||
| 225 | } | ||
| 226 | } | ||
| 227 | return undef; | ||
| 228 | } | ||
| 229 | sub _xy_to_d { | ||
| 230 | my ($x, $y) = @_; | ||
| 231 | ### _xy_to_d(): "$x,$y" | ||
| 232 | |||
| 233 | if ($x >= abs($y)-2) { | ||
| 234 | ### right ... | ||
| 235 | return int(($x+2)/4); | ||
| 236 | } | ||
| 237 | if ($x <= -abs($y)) { | ||
| 238 | ### left ... | ||
| 239 | return int((-1-$x)/4); | ||
| 240 | } | ||
| 241 | ### vertical ... | ||
| 242 | return int((abs($y)-1)/4); | ||
| 243 | } | ||
| 244 | |||
| 245 | |||
| 246 | # not exact | ||
| 247 | sub rect_to_n_range { | ||
| 248 | my ($self, $x1,$y1, $x2,$y2) = @_; | ||
| 249 | ### CretanLabyrinth rect_to_n_range(): "$x1,$y1 $x2,$y2" | ||
| 250 | |||
| 251 | $x1 = round_nearest($x1); | ||
| 252 | $y1 = round_nearest($y1); | ||
| 253 | $x2 = round_nearest($x2); | ||
| 254 | $y2 = round_nearest($y2); | ||
| 255 | |||
| 256 | my $d = max (_xy_to_d($x1,$y1), | ||
| 257 | _xy_to_d($x2,$y1), | ||
| 258 | _xy_to_d($x1,$y2), | ||
| 259 | _xy_to_d($x2,$y2)); | ||
| 260 | return (1, | ||
| 261 | (64*$d + 120)*$d + 54); | ||
| 262 | } | ||
| 263 | |||
| 264 | 1; | ||
| 265 | __END__ |