| 1 | # Copyright 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020 Kevin Ryde | ||
| 2 | |||
| 3 | # This file is part of Math-PlanePath. | ||
| 4 | # | ||
| 5 | # Math-PlanePath is free software; you can redistribute it and/or modify | ||
| 6 | # it under the terms of the GNU General Public License as published by the | ||
| 7 | # Free Software Foundation; either version 3, or (at your option) any later | ||
| 8 | # version. | ||
| 9 | # | ||
| 10 | # Math-PlanePath is distributed in the hope that it will be useful, but | ||
| 11 | # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY | ||
| 12 | # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | ||
| 13 | # for more details. | ||
| 14 | # | ||
| 15 | # You should have received a copy of the GNU General Public License along | ||
| 16 | # with Math-PlanePath. If not, see <http://www.gnu.org/licenses/>. | ||
| 17 | |||
| 18 | |||
| 19 | package Math::PlanePath::R5DragonCurve; | ||
| 20 | use 5.004; | ||
| 21 | use strict; | ||
| 22 | use List::Util 'first','sum'; | ||
| 23 | use List::Util 'min'; # 'max' | ||
| 24 | *max = \&Math::PlanePath::_max; | ||
| 25 | |||
| 26 | use vars '$VERSION', '@ISA'; | ||
| 27 | $VERSION = 128; | ||
| 28 | use Math::PlanePath; | ||
| 29 | use Math::PlanePath::Base::NSEW; | ||
| 30 | @ISA = ('Math::PlanePath::Base::NSEW', | ||
| 31 | 'Math::PlanePath'); | ||
| 32 | |||
| 33 | use Math::PlanePath::Base::Generic | ||
| 34 | 'is_infinite', | ||
| 35 | 'round_nearest'; | ||
| 36 | use Math::PlanePath::Base::Digits | ||
| 37 | 'digit_split_lowtohigh', | ||
| 38 | 'round_up_pow'; | ||
| 39 | *_divrem_mutate = \&Math::PlanePath::_divrem_mutate; | ||
| 40 | |||
| 41 | # uncomment this to run the ### lines | ||
| 42 | # use Smart::Comments; | ||
| 43 | |||
| 44 | |||
| 45 | use constant n_start => 0; | ||
| 46 | use constant parameter_info_array => | ||
| 47 | [ { name => 'arms', | ||
| 48 | share_key => 'arms_4', | ||
| 49 | display => 'Arms', | ||
| 50 | type => 'integer', | ||
| 51 | minimum => 1, | ||
| 52 | maximum => 4, | ||
| 53 | default => 1, | ||
| 54 | width => 1, | ||
| 55 | description => 'Arms', | ||
| 56 | } ]; | ||
| 57 | |||
| 58 | { | ||
| 59 | my @x_negative_at_n = (undef, 9,5,5,6); | ||
| 60 | sub x_negative_at_n { | ||
| 61 | my ($self) = @_; | ||
| 62 | return $x_negative_at_n[$self->{'arms'}]; | ||
| 63 | } | ||
| 64 | } | ||
| 65 | { | ||
| 66 | my @y_negative_at_n = (undef, 54,19,8,7); | ||
| 67 | sub y_negative_at_n { | ||
| 68 | my ($self) = @_; | ||
| 69 | return $y_negative_at_n[$self->{'arms'}]; | ||
| 70 | } | ||
| 71 | } | ||
| 72 | |||
| 73 | use constant turn_any_straight => 0; # never straight | ||
| 74 | |||
| 75 | |||
| 76 | #------------------------------------------------------------------------------ | ||
| 77 | |||
| 78 | sub new { | ||
| 79 | my $self = shift->SUPER::new(@_); | ||
| 80 | $self->{'arms'} = max(1, min(4, $self->{'arms'} || 1)); | ||
| 81 | return $self; | ||
| 82 | } | ||
| 83 | |||
| 84 | sub n_to_xy { | ||
| 85 | my ($self, $n) = @_; | ||
| 86 | ### R5dragonCurve n_to_xy(): $n | ||
| 87 | |||
| 88 | if ($n < 0) { return; } | ||
| 89 | if (is_infinite($n)) { return ($n, $n); } | ||
| 90 | |||
| 91 | my $int = int($n); | ||
| 92 | $n -= $int; # fraction part | ||
| 93 | |||
| 94 | my $zero = ($n * 0); # inherit bignum 0 | ||
| 95 | my $one = $zero + 1; # inherit bignum 1 | ||
| 96 | |||
| 97 | my $x = 0; | ||
| 98 | my $y = 0; | ||
| 99 | my $sx = $zero; | ||
| 100 | my $sy = $zero; | ||
| 101 | |||
| 102 | # initial rotation from arm number | ||
| 103 | { | ||
| 104 | my $rot = _divrem_mutate ($int, $self->{'arms'}); | ||
| 105 | if ($rot == 0) { $x = $n; $sx = $one; } | ||
| 106 | elsif ($rot == 1) { $y = $n; $sy = $one; } | ||
| 107 | elsif ($rot == 2) { $x = -$n; $sx = -$one; } | ||
| 108 | else { $y = -$n; $sy = -$one; } # rot==3 | ||
| 109 | } | ||
| 110 | |||
| 111 | foreach my $digit (digit_split_lowtohigh($int,5)) { | ||
| 112 | |||
| 113 | ### at: "$x,$y side $sx,$sy" | ||
| 114 | ### $digit | ||
| 115 | |||
| 116 | if ($digit == 1) { | ||
| 117 | ($x,$y) = ($sx-$y, $sy+$x); # rotate +90 and offset | ||
| 118 | } elsif ($digit == 2) { | ||
| 119 | $x = $sx-$sy - $x; # rotate 180 and offset diag | ||
| 120 | $y = $sy+$sx - $y; | ||
| 121 | } elsif ($digit == 3) { | ||
| 122 | ($x,$y) = (-$sy - $y, $sx + $x); # rotate +90 and offset vert | ||
| 123 | } elsif ($digit == 4) { | ||
| 124 | $x -= 2*$sy; # offset vert 2* | ||
| 125 | $y += 2*$sx; | ||
| 126 | } | ||
| 127 | |||
| 128 | # add 2*(rot+90), which is multiply by (2i+1) | ||
| 129 | ($sx,$sy) = ($sx - 2*$sy, | ||
| 130 | $sy + 2*$sx); | ||
| 131 | } | ||
| 132 | |||
| 133 | ### final: "$x,$y side $sx,$sy" | ||
| 134 | |||
| 135 | return ($x, $y); | ||
| 136 | } | ||
| 137 | |||
| 138 | my @digit_to_dir = (0,1,2,1,0); | ||
| 139 | my @dir4_to_dx = (1,0,-1,0); | ||
| 140 | my @dir4_to_dy = (0,1,0,-1); | ||
| 141 | my @digit_to_nextturn = (1,1,-1,-1); | ||
| 142 | |||
| 143 | sub n_to_dxdy { | ||
| 144 | my ($self, $n) = @_; | ||
| 145 | ### R5dragonCurve n_to_dxdy(): $n | ||
| 146 | |||
| 147 | if ($n < 0) { return; } | ||
| 148 | |||
| 149 | my $int = int($n); | ||
| 150 | $n -= $int; # fraction part | ||
| 151 | |||
| 152 | if (is_infinite($int)) { return ($int, $int); } | ||
| 153 | |||
| 154 | # direction from arm number | ||
| 155 | my $dir = _divrem_mutate ($int, $self->{'arms'}); | ||
| 156 | |||
| 157 | # plus direction from digits | ||
| 158 | my @ndigits = digit_split_lowtohigh($int,5); | ||
| 159 | $dir = sum($dir, map {$digit_to_dir[$_]} @ndigits) & 3; | ||
| 160 | |||
| 161 | ### direction: $dir | ||
| 162 | my $dx = $dir4_to_dx[$dir]; | ||
| 163 | my $dy = $dir4_to_dy[$dir]; | ||
| 164 | |||
| 165 | # fractional $n incorporated using next turn | ||
| 166 | if ($n) { | ||
| 167 | # lowest non-4 digit, or 0 if all 4s (implicit 0 above high digit) | ||
| 168 | $dir += $digit_to_nextturn[ first {$_!=4} @ndigits, 0 ]; | ||
| 169 | $dir &= 3; | ||
| 170 | ### next direction: $dir | ||
| 171 | $dx += $n*($dir4_to_dx[$dir] - $dx); | ||
| 172 | $dy += $n*($dir4_to_dy[$dir] - $dy); | ||
| 173 | } | ||
| 174 | return ($dx, $dy); | ||
| 175 | } | ||
| 176 | |||
| 177 | sub xy_to_n { | ||
| 178 | return scalar((shift->xy_to_n_list(@_))[0]); | ||
| 179 | } | ||
| 180 | sub xy_to_n_list { | ||
| 181 | my ($self, $x, $y) = @_; | ||
| 182 | ### R5DragonCurve xy_to_n(): "$x, $y" | ||
| 183 | |||
| 184 | $x = round_nearest($x); | ||
| 185 | $y = round_nearest($y); | ||
| 186 | |||
| 187 | if (is_infinite($x)) { | ||
| 188 | return $x; # infinity | ||
| 189 | } | ||
| 190 | if (is_infinite($y)) { | ||
| 191 | return $y; # infinity | ||
| 192 | } | ||
| 193 | |||
| 194 | if ($x == 0 && $y == 0) { | ||
| 195 | return (0 .. $self->arms_count - 1); | ||
| 196 | } | ||
| 197 | |||
| 198 | require Math::PlanePath::R5DragonMidpoint; | ||
| 199 | |||
| 200 | my @n_list; | ||
| 201 | my $xm = $x+$y; # rotate -45 and mul sqrt(2) | ||
| 202 | my $ym = $y-$x; | ||
| 203 | foreach my $dx (0,-1) { | ||
| 204 | foreach my $dy (0,1) { | ||
| 205 | my $t = $self->Math::PlanePath::R5DragonMidpoint::xy_to_n | ||
| 206 | ($xm+$dx, $ym+$dy); | ||
| 207 | |||
| 208 | ### try: ($xm+$dx).",".($ym+$dy) | ||
| 209 | ### $t | ||
| 210 | next unless defined $t; | ||
| 211 | |||
| 212 | my ($tx,$ty) = $self->n_to_xy($t) | ||
| 213 | or next; | ||
| 214 | |||
| 215 | if ($tx == $x && $ty == $y) { | ||
| 216 | ### found: $t | ||
| 217 | if (@n_list && $t < $n_list[0]) { | ||
| 218 | unshift @n_list, $t; | ||
| 219 | } else { | ||
| 220 | push @n_list, $t; | ||
| 221 | } | ||
| 222 | if (@n_list == 2) { | ||
| 223 | return @n_list; | ||
| 224 | } | ||
| 225 | } | ||
| 226 | } | ||
| 227 | } | ||
| 228 | return @n_list; | ||
| 229 | } | ||
| 230 | |||
| 231 | #------------------------------------------------------------------------------ | ||
| 232 | |||
| 233 | # whole plane covered when arms==4 | ||
| 234 | sub xy_is_visited { | ||
| 235 | my ($self, $x, $y) = @_; | ||
| 236 | return ($self->{'arms'} == 4 | ||
| 237 | || defined($self->xy_to_n($x,$y))); | ||
| 238 | } | ||
| 239 | |||
| 240 | #------------------------------------------------------------------------------ | ||
| 241 | |||
| 242 | # not exact | ||
| 243 | sub rect_to_n_range { | ||
| 244 | my ($self, $x1,$y1, $x2,$y2) = @_; | ||
| 245 | ### R5DragonCurve rect_to_n_range(): "$x1,$y1 $x2,$y2" | ||
| 246 | my $xmax = int(max(abs($x1),abs($x2))) + 1; | ||
| 247 | my $ymax = int(max(abs($y1),abs($y2))) + 1; | ||
| 248 | return (0, | ||
| 249 | ($xmax*$xmax + $ymax*$ymax) | ||
| 250 | * 10 | ||
| 251 | * $self->{'arms'}); | ||
| 252 | } | ||
| 253 | |||
| 254 | #------------------------------------------------------------------------------ | ||
| 255 | { | ||
| 256 | # This is a search for disallowed digit pairs going from low to high. | ||
| 257 | # $state encodes the preceding digit, ie. of lower significance. Initial | ||
| 258 | # $state=1 for no low digits yet. The initial no low digits skips low | ||
| 259 | # digit=1 and then begins the allowing/disallowing on the first non-1 | ||
| 260 | # digit. | ||
| 261 | # | ||
| 262 | # The digits are found by repeated _divrem_mutate() in the expectation | ||
| 263 | # that with 8 out of 20 digit pairs disallowed, after stripping low 1s, we | ||
| 264 | # should be able to usually answer "no" in less work than a full | ||
| 265 | # digit_split_lowtohigh(), and since currently that code for base 5 is | ||
| 266 | # only repeated divrems anyway. | ||
| 267 | # | ||
| 268 | my @table | ||
| 269 | = (undef, # state prev allowed pairs | ||
| 270 | # # ----- ---- ------------- | ||
| 271 | [ 2, 1, 3, 4, 5 ], # 1 none | ||
| 272 | [ 2, 2, 3 ], # 2 0 00, 20 | ||
| 273 | [ 2, 3, undef, undef, 5 ], # 3 2 02, 42 | ||
| 274 | [ 2, 4, undef, undef, 5 ], # 4 3 03, 43 | ||
| 275 | [ 2, 5, undef, undef, 5 ], # 5 4 04, 44 | ||
| 276 | ); | ||
| 277 | |||
| 278 | sub _UNDOCUMENTED__n_segment_is_right_boundary { | ||
| 279 | my ($self, $n) = @_; | ||
| 280 | if (is_infinite($n)) { return 0; } | ||
| 281 | unless ($n >= 0) { return 0; } | ||
| 282 | $n = int($n); | ||
| 283 | |||
| 284 | my $state = 1; | ||
| 285 | while ($n) { | ||
| 286 | my $digit = _divrem_mutate($n,5); # low to high | ||
| 287 | $state = $table[$state][$digit] || return 0; | ||
| 288 | } | ||
| 289 | return 1; | ||
| 290 | } | ||
| 291 | |||
| 292 | sub _UNDOCUMENTED__n_segment_is_left_boundary { | ||
| 293 | my ($self, $n, $level) = @_; | ||
| 294 | ### _UNDOCUMENTED__n_segment_is_left_boundary(): $n | ||
| 295 | if (is_infinite($n)) { return 0; } | ||
| 296 | unless ($n >= 0) { return 0; } | ||
| 297 | $n = int($n); | ||
| 298 | |||
| 299 | my $state = 1; | ||
| 300 | while ($n) { | ||
| 301 | if (defined $level && ($level -= 1) < 0) { | ||
| 302 | ### stop at level: "state=$state" | ||
| 303 | if ($n) { | ||
| 304 | ### N >= 5**$level ... | ||
| 305 | return undef; | ||
| 306 | } | ||
| 307 | last; | ||
| 308 | # return 1; | ||
| 309 | # return ($state == 2); | ||
| 310 | } | ||
| 311 | my $digit = 4 - _divrem_mutate($n,5); # low to high | ||
| 312 | $state = $table[$state][$digit] || return 0; | ||
| 313 | } | ||
| 314 | ### final state: $state | ||
| 315 | if (defined $level) { | ||
| 316 | if ($level > 0) { | ||
| 317 | return ($state != 2); | ||
| 318 | } else { | ||
| 319 | return 1; | ||
| 320 | } | ||
| 321 | } | ||
| 322 | return ($state != 2); | ||
| 323 | |||
| 324 | # my @table | ||
| 325 | # # 0 1 2 3 4 digit | ||
| 326 | # = (undef, | ||
| 327 | # [ 4, 3, 2, 1, 1 ], # 1 L -> ZYXLL | ||
| 328 | # [undef,undef,undef, 2, 1 ], # 2 X -> ___XL | ||
| 329 | # [undef,undef,undef, 3 ], # 3 Y -> ___Y_ | ||
| 330 | # [ 4, 3, 2, 4 ], # 4 Z -> ZYXX_ | ||
| 331 | # ); | ||
| 332 | # my $state = 4; | ||
| 333 | # foreach my $digit (reverse digit_split_lowtohigh($n,5)) { # high to low | ||
| 334 | # $state = $table[$state][$digit] || return 0; | ||
| 335 | # } | ||
| 336 | # return 1; | ||
| 337 | } | ||
| 338 | } | ||
| 339 | |||
| 340 | |||
| 341 | #----------------------------------------------------------------------------- | ||
| 342 | # level_to_n_range() | ||
| 343 | |||
| 344 | sub level_to_n_range { | ||
| 345 | my ($self, $level) = @_; | ||
| 346 | return (0, (5**$level + 1) * $self->{'arms'} - 1); | ||
| 347 | } | ||
| 348 | sub n_to_level { | ||
| 349 | my ($self, $n) = @_; | ||
| 350 | if ($n < 0) { return undef; } | ||
| 351 | if (is_infinite($n)) { return $n; } | ||
| 352 | $n = round_nearest($n); | ||
| 353 | _divrem_mutate ($n, $self->{'arms'}); | ||
| 354 | my ($pow, $exp) = round_up_pow ($n, 5); | ||
| 355 | return $exp; | ||
| 356 | } | ||
| 357 | |||
| 358 | #----------------------------------------------------------------------------- | ||
| 359 | 1; | ||
| 360 | __END__ |