| 1 | # Copyright 2011, 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 | # Archive of all Wunderlich's papers: | ||
| 20 | # http://sodwana.uni-ak.ac.at/geom/mitarbeiter/wallner/wunderlich/ | ||
| 21 | # http://sodwana.uni-ak.ac.at/geom/mitarbeiter/wallner/wunderlich/pdf/125.pdf | ||
| 22 | # [8.5mb] | ||
| 23 | # https://eudml.org/doc/141086 | ||
| 24 | # | ||
| 25 | # Walter Wunderlich. Uber Peano-Kurven. Elemente der Mathematik, 28(1):1-10, | ||
| 26 | # 1973. | ||
| 27 | # | ||
| 28 | # Coil order 111 111 111 | ||
| 29 | # | ||
| 30 | # math-image --path=WunderlichSerpentine --all --output=numbers_dash | ||
| 31 | # math-image --path=WunderlichSerpentine,radix=5 --all --output=numbers_dash | ||
| 32 | # math-image --path=WunderlichSerpentine,serpentine_type=170 --all --output=numbers_dash | ||
| 33 | # | ||
| 34 | # | ||
| 35 | # cf diagonals of alternating | ||
| 36 | # turn(n) = n=n/9^valuation(n,9); [-1,1,1,1,-1,-1,-1,1][n%9]; | ||
| 37 | # vector(20,n,turn(n)) | ||
| 38 | # not in OEIS: -1,1,1,1,-1,-1,-1,1,-1,-1,1,1,1,-1,-1,-1,1,1,-1,1 | ||
| 39 | # vector(20,n,turn(9*n+3)) == vector(20,n, 1) | ||
| 40 | # vector(20,n,turn(9*n+6)) == vector(20,n, -1) | ||
| 41 | # [-1,1, 1, 1,-1, -1, -1,1] | ||
| 42 | # 1 2 0 1 2 0 1 2 n mod 3 | ||
| 43 | # 01,02 11,12, 21,22 | ||
| 44 | |||
| 45 | |||
| 46 | package Math::PlanePath::WunderlichSerpentine; | ||
| 47 | use 5.004; | ||
| 48 | use strict; | ||
| 49 | use Carp 'croak'; | ||
| 50 | #use List::Util 'max'; | ||
| 51 | *max = \&Math::PlanePath::_max; | ||
| 52 | |||
| 53 | use vars '$VERSION', '@ISA'; | ||
| 54 | $VERSION = 128; | ||
| 55 | use Math::PlanePath; | ||
| 56 | @ISA = ('Math::PlanePath'); | ||
| 57 | *_divrem = \&Math::PlanePath::_divrem; | ||
| 58 | |||
| 59 | use Math::PlanePath::Base::Generic | ||
| 60 | 'is_infinite', | ||
| 61 | 'round_nearest'; | ||
| 62 | use Math::PlanePath::Base::Digits | ||
| 63 | 'round_down_pow', | ||
| 64 | 'digit_split_lowtohigh'; | ||
| 65 | |||
| 66 | # uncomment this to run the ### lines | ||
| 67 | #use Smart::Comments; | ||
| 68 | |||
| 69 | |||
| 70 | use constant n_start => 0; | ||
| 71 | use constant class_x_negative => 0; | ||
| 72 | use constant class_y_negative => 0; | ||
| 73 | *xy_is_visited = \&Math::PlanePath::Base::Generic::xy_is_visited_quad1; | ||
| 74 | |||
| 75 | use constant parameter_info_array => | ||
| 76 | [ { name => 'serpentine_type', | ||
| 77 | display => 'Serpentine Type', | ||
| 78 | type => 'string', | ||
| 79 | default => '010 101 010', | ||
| 80 | choices => ['alternating','coil','Peano'], | ||
| 81 | width => 11, | ||
| 82 | type_hint => 'bit_string', | ||
| 83 | description => 'Serpentine type, as a bit string or one of the predefined choices.', | ||
| 84 | }, | ||
| 85 | { name => 'radix', | ||
| 86 | share_key => 'radix_3', | ||
| 87 | display => 'Radix', | ||
| 88 | type => 'integer', | ||
| 89 | minimum => 2, | ||
| 90 | default => 3, | ||
| 91 | width => 3, | ||
| 92 | }, | ||
| 93 | ]; | ||
| 94 | |||
| 95 | |||
| 96 | # same as PeanoCurve | ||
| 97 | use Math::PlanePath::PeanoCurve; | ||
| 98 | *dx_minimum = Math::PlanePath::PeanoCurve->can('dx_minimum'); | ||
| 99 | *dx_maximum = Math::PlanePath::PeanoCurve->can('dx_maximum'); | ||
| 100 | *dy_minimum = Math::PlanePath::PeanoCurve->can('dy_minimum'); | ||
| 101 | *dy_maximum = Math::PlanePath::PeanoCurve->can('dy_maximum'); | ||
| 102 | |||
| 103 | *_UNDOCUMENTED__dxdy_list = Math::PlanePath::PeanoCurve->can('_UNDOCUMENTED__dxdy_list'); # same | ||
| 104 | # | ||
| 105 | # bit=0 bit=1 | ||
| 106 | # *--- b^2-1 -- b^2 | ||
| 107 | # | | | ||
| 108 | # *------- | | ||
| 109 | # | | | ||
| 110 | # 0 ----- b | ||
| 111 | # | ||
| 112 | # bit=0 bit=0 | ||
| 113 | # *--- b^2-1 -- b^2 ---- b^2+b-1 | ||
| 114 | # | | | ||
| 115 | # *------- | ||
| 116 | # | | ||
| 117 | # 0 ----- b | ||
| 118 | # | ||
| 119 | # *-------- | ||
| 120 | # | | ||
| 121 | # *-------* bit=1 | ||
| 122 | # | | ||
| 123 | # *--- b^2-1 | ||
| 124 | # | | ||
| 125 | # *------- bit=1 | ||
| 126 | # | | ||
| 127 | # 0 ----- b | ||
| 128 | # | ||
| 129 | sub _UNDOCUMENTED__dxdy_list_at_n { | ||
| 130 | my ($self) = @_; | ||
| 131 | my $radix = $self->{'radix'}; | ||
| 132 | my $n = $radix*$radix; | ||
| 133 | if ($self->{'serpentine_array'}->[0]) { | ||
| 134 | foreach my $i (1 .. $self->{'radix'}-1) { | ||
| 135 | if ($self->{'serpentine_array'}->[$i] == 0) { | ||
| 136 | return $n*$i + $radix; | ||
| 137 | } | ||
| 138 | } | ||
| 139 | if ($self->{'serpentine_array'}->[$radix] == 0) { | ||
| 140 | return $n*$radix; | ||
| 141 | } else { | ||
| 142 | return $n*$radix + $radix-1; | ||
| 143 | } | ||
| 144 | } else { | ||
| 145 | if ($self->{'serpentine_array'}->[1]) { | ||
| 146 | return $n; | ||
| 147 | } else { | ||
| 148 | return $n + $radix-1; | ||
| 149 | } | ||
| 150 | } | ||
| 151 | } | ||
| 152 | |||
| 153 | *dsumxy_minimum = \&dx_minimum; | ||
| 154 | *dsumxy_maximum = \&dx_maximum; | ||
| 155 | *ddiffxy_minimum = \&dy_minimum; | ||
| 156 | *ddiffxy_maximum = \&dy_maximum; | ||
| 157 | |||
| 158 | # radix=2 0101 is straight NSEW parts, other evens are diagonal | ||
| 159 | sub dir_maximum_dxdy { | ||
| 160 | my ($self) = @_; | ||
| 161 | return (($self->{'radix'} % 2) | ||
| 162 | || join('',@{$self->{'serpentine_array'}}) eq '0101' | ||
| 163 | ? (0,-1) # odd, South | ||
| 164 | : (0,0)); # even, supremum | ||
| 165 | } | ||
| 166 | |||
| 167 | |||
| 168 | #------------------------------------------------------------------------------ | ||
| 169 | |||
| 170 | sub new { | ||
| 171 | my $self = shift->SUPER::new(@_); | ||
| 172 | |||
| 173 | my $radix = $self->{'radix'}; | ||
| 174 | if (! $self->{'radix'} || $self->{'radix'} < 2) { | ||
| 175 | $radix = $self->{'radix'} = 3; | ||
| 176 | } | ||
| 177 | |||
| 178 | my @serpentine_array; | ||
| 179 | |||
| 180 | my $serpentine_type = $self->{'serpentine_type'}; | ||
| 181 | if (! defined $serpentine_type) { | ||
| 182 | $serpentine_type = 'alternating'; | ||
| 183 | } | ||
| 184 | $serpentine_type = lc($serpentine_type); | ||
| 185 | ### $serpentine_type | ||
| 186 | |||
| 187 | if ($serpentine_type eq 'alternating') { | ||
| 188 | @serpentine_array = map {$_&1} 0 .. $radix*$radix - 1; | ||
| 189 | |||
| 190 | } elsif ($serpentine_type eq 'coil') { | ||
| 191 | @serpentine_array = (1) x ($radix*$radix); | ||
| 192 | |||
| 193 | } elsif (lc($serpentine_type) eq 'peano') { | ||
| 194 | @serpentine_array = (0) x ($radix*$radix); | ||
| 195 | |||
| 196 | } elsif ($serpentine_type =~ /^([01_,.]|\s)*$/) { | ||
| 197 | # bits 010,101,010 etc | ||
| 198 | $serpentine_type =~ tr/01//cd; # keep only 0,1 chars | ||
| 199 | @serpentine_array | ||
| 200 | = map {$_+0} # numize for xor | ||
| 201 | split //, $serpentine_type; # each char, 0,1 | ||
| 202 | push @serpentine_array, | ||
| 203 | (0) x max(0,$radix*$radix-scalar(@serpentine_array)); | ||
| 204 | |||
| 205 | # foreach my $char { | ||
| 206 | # if ($char eq '0' || $char eq '1') { | ||
| 207 | # push , $char; | ||
| 208 | # } | ||
| 209 | # my @parts = split /[^01]+/, $serpentine_type; | ||
| 210 | # ### @parts | ||
| 211 | # @parts = grep {$_ ne ''} @parts; # no empty parts | ||
| 212 | # foreach my $part (@parts) { | ||
| 213 | # my @bits = split //, $part; | ||
| 214 | # push @bits, (0) x max(0,scalar(@bits)-$radix); | ||
| 215 | # $#bits = $radix-1; | ||
| 216 | # push @serpentine_array, @bits; # radix many row | ||
| 217 | # } | ||
| 218 | |||
| 219 | } else { | ||
| 220 | croak "Unrecognised serpentine_type \"$serpentine_type\""; | ||
| 221 | } | ||
| 222 | |||
| 223 | ### @serpentine_array | ||
| 224 | $self->{'serpentine_array'} = \@serpentine_array; | ||
| 225 | |||
| 226 | return $self; | ||
| 227 | } | ||
| 228 | |||
| 229 | sub n_to_xy { | ||
| 230 | my ($self, $n) = @_; | ||
| 231 | ### WunderlichSerpentine n_to_xy(): $n | ||
| 232 | |||
| 233 | if ($n < 0) { | ||
| 234 | return; | ||
| 235 | } | ||
| 236 | if (is_infinite($n)) { | ||
| 237 | return ($n,$n); | ||
| 238 | } | ||
| 239 | |||
| 240 | { | ||
| 241 | # ENHANCE-ME: for odd radix the ends join and the direction can be had | ||
| 242 | # without a full N+1 calculation | ||
| 243 | my $int = int($n); | ||
| 244 | ### $int | ||
| 245 | ### $n | ||
| 246 | if ($n != $int) { | ||
| 247 | my ($x1,$y1) = $self->n_to_xy($int); | ||
| 248 | my ($x2,$y2) = $self->n_to_xy($int+1); | ||
| 249 | my $frac = $n - $int; # inherit possible BigFloat | ||
| 250 | my $dx = $x2-$x1; | ||
| 251 | my $dy = $y2-$y1; | ||
| 252 | return ($frac*$dx + $x1, $frac*$dy + $y1); | ||
| 253 | } | ||
| 254 | $n = $int; # BigFloat int() gives BigInt, use that | ||
| 255 | } | ||
| 256 | |||
| 257 | # high to low | ||
| 258 | my $radix = $self->{'radix'}; | ||
| 259 | my $rsquared = $radix * $radix; | ||
| 260 | my $radix_minus_1 = $radix - 1; | ||
| 261 | my $serpentine_array = $self->{'serpentine_array'}; | ||
| 262 | |||
| 263 | my @digits = digit_split_lowtohigh($n,$rsquared); | ||
| 264 | my $x = 0; | ||
| 265 | my $y = 0; | ||
| 266 | |||
| 267 | my $transpose = ($#digits & 1) && $serpentine_array->[0]; | ||
| 268 | my $xk = my $yk = 0; | ||
| 269 | while (@digits) { | ||
| 270 | my $ndigit = pop @digits; # high to low | ||
| 271 | my ($highdigit, $lowdigit) = _divrem ($ndigit, $radix); | ||
| 272 | |||
| 273 | ### $lowdigit | ||
| 274 | ### $highdigit | ||
| 275 | |||
| 276 | if ($transpose) { | ||
| 277 | $yk ^= $highdigit; | ||
| 278 | $x *= $radix; | ||
| 279 | $x += ($xk & 1 ? $radix_minus_1-$highdigit : $highdigit); | ||
| 280 | |||
| 281 | $xk ^= $lowdigit; | ||
| 282 | $y *= $radix; | ||
| 283 | $y += ($yk & 1 ? $radix_minus_1-$lowdigit : $lowdigit); | ||
| 284 | } else { | ||
| 285 | $xk ^= $highdigit; | ||
| 286 | $y *= $radix; | ||
| 287 | $y += ($yk & 1 ? $radix_minus_1-$highdigit : $highdigit); | ||
| 288 | |||
| 289 | $yk ^= $lowdigit; | ||
| 290 | $x *= $radix; | ||
| 291 | $x += ($xk & 1 ? $radix_minus_1-$lowdigit : $lowdigit); | ||
| 292 | } | ||
| 293 | ### $serpentine_array | ||
| 294 | ### $ndigit | ||
| 295 | $transpose ^= $serpentine_array->[$ndigit]; | ||
| 296 | } | ||
| 297 | return ($x, $y); | ||
| 298 | |||
| 299 | |||
| 300 | |||
| 301 | |||
| 302 | # my $x = 0; | ||
| 303 | # my $y = 0; | ||
| 304 | # my $power = $x + 1; # inherit bignum 1 | ||
| 305 | # my $odd = 1; | ||
| 306 | # my $transpose = 0; | ||
| 307 | # while ($n) { | ||
| 308 | # $odd ^= 1; | ||
| 309 | # ### $n | ||
| 310 | # ### $power | ||
| 311 | # | ||
| 312 | # my $xdigit = $n % $radix; | ||
| 313 | # $n = int($n/$radix); | ||
| 314 | # my $ydigit = $n % $radix; | ||
| 315 | # $n = int($n/$radix); | ||
| 316 | # | ||
| 317 | # if ($transpose) { | ||
| 318 | # ($xdigit,$ydigit) = ($ydigit,$xdigit); | ||
| 319 | # } | ||
| 320 | # $transpose ^= $serpentine_array->[$xdigit + $radix*$ydigit]; | ||
| 321 | # | ||
| 322 | # ### $xdigit | ||
| 323 | # ### $ydigit | ||
| 324 | # | ||
| 325 | # if ($xdigit & 1) { | ||
| 326 | # $y = $power-1 - $y; # 99..99 - Y | ||
| 327 | # } | ||
| 328 | # $x += $power * $xdigit; | ||
| 329 | # | ||
| 330 | # $y += $power * $ydigit; | ||
| 331 | # $power *= $radix; | ||
| 332 | # if ($ydigit & 1) { | ||
| 333 | # $x = $power-1 - $x; | ||
| 334 | # } | ||
| 335 | # } | ||
| 336 | # | ||
| 337 | # # | ||
| 338 | # # if ($odd) { | ||
| 339 | # # ($x,$y) = ($y,$x); | ||
| 340 | # # ### final transpose to: "$x,$y" | ||
| 341 | # # } | ||
| 342 | # | ||
| 343 | # return ($x, $y); | ||
| 344 | } | ||
| 345 | |||
| 346 | sub xy_to_n { | ||
| 347 | my ($self, $x, $y) = @_; | ||
| 348 | ### WunderlichSerpentine xy_to_n(): "$x, $y" | ||
| 349 | |||
| 350 | $x = round_nearest ($x); | ||
| 351 | $y = round_nearest ($y); | ||
| 352 | if ($x < 0 || $y < 0 | ||
| 353 | || is_infinite($x) | ||
| 354 | || is_infinite($y)) { | ||
| 355 | return undef; | ||
| 356 | } | ||
| 357 | |||
| 358 | my $radix = $self->{'radix'}; | ||
| 359 | my $radix_minus_1 = $radix - 1; | ||
| 360 | |||
| 361 | my @xdigits = digit_split_lowtohigh($x,$radix); | ||
| 362 | my @ydigits = digit_split_lowtohigh($y,$radix); | ||
| 363 | ### @xdigits | ||
| 364 | ### @ydigits | ||
| 365 | |||
| 366 | my $serpentine_array = $self->{'serpentine_array'}; | ||
| 367 | my $xk = 0; | ||
| 368 | my $yk = 0; | ||
| 369 | my $highpos = max($#xdigits,$#ydigits); | ||
| 370 | my $transpose = $serpentine_array->[0] && ($highpos & 1); | ||
| 371 | |||
| 372 | my $n = ($x * 0 * $y); # inherit bignum 0 | ||
| 373 | |||
| 374 | foreach my $i (reverse 0 .. $highpos) { # high to low | ||
| 375 | my $xdigit = $xdigits[$i] || 0; | ||
| 376 | my $ydigit = $ydigits[$i] || 0; | ||
| 377 | my $ndigit; | ||
| 378 | |||
| 379 | ### $n | ||
| 380 | ### $xk | ||
| 381 | ### $yk | ||
| 382 | ### $transpose | ||
| 383 | ### $xdigit | ||
| 384 | ### $ydigit | ||
| 385 | |||
| 386 | if ($transpose) { | ||
| 387 | if ($xk & 1) { $xdigit = $radix_minus_1 - $xdigit; } | ||
| 388 | $n *= $radix; | ||
| 389 | $n += $xdigit; | ||
| 390 | $yk ^= $xdigit; | ||
| 391 | |||
| 392 | if ($yk & 1) { $ydigit = $radix_minus_1 - $ydigit; } | ||
| 393 | $n *= $radix; | ||
| 394 | $n += $ydigit; | ||
| 395 | $xk ^= $ydigit; | ||
| 396 | |||
| 397 | $ndigit = $radix*$xdigit + $ydigit; | ||
| 398 | |||
| 399 | } else { | ||
| 400 | if ($yk & 1) { $ydigit = $radix_minus_1 - $ydigit; } | ||
| 401 | $n *= $radix; | ||
| 402 | $n += $ydigit; | ||
| 403 | $xk ^= $ydigit; | ||
| 404 | |||
| 405 | if ($xk & 1) { $xdigit = $radix_minus_1 - $xdigit; } | ||
| 406 | $n *= $radix; | ||
| 407 | $n += $xdigit; | ||
| 408 | $yk ^= $xdigit; | ||
| 409 | |||
| 410 | $ndigit = $radix*$ydigit + $xdigit; | ||
| 411 | } | ||
| 412 | |||
| 413 | ### ndigits: "$ydigit, $xdigit" | ||
| 414 | $transpose ^= $serpentine_array->[$ndigit]; | ||
| 415 | } | ||
| 416 | return $n; | ||
| 417 | |||
| 418 | |||
| 419 | # my $n = 0; | ||
| 420 | # while (@x) { | ||
| 421 | # if (($xk ^ $yk) & 1) { | ||
| 422 | # { | ||
| 423 | # my $digit = pop @x; | ||
| 424 | # if ($xk & 1) { | ||
| 425 | # $digit = $radix_minus_1 - $digit; | ||
| 426 | # } | ||
| 427 | # $n = ($n * $radix) + $digit; | ||
| 428 | # $yk ^= $digit; | ||
| 429 | # } | ||
| 430 | # { | ||
| 431 | # my $digit = pop @y; | ||
| 432 | # if ($yk & 1) { | ||
| 433 | # $digit = $radix_minus_1 - $digit; | ||
| 434 | # } | ||
| 435 | # $n = ($n * $radix) + $digit; | ||
| 436 | # $xk ^= $digit; | ||
| 437 | # } | ||
| 438 | # } else { | ||
| 439 | # { | ||
| 440 | # my $digit = pop @y; | ||
| 441 | # if ($yk & 1) { | ||
| 442 | # $digit = $radix_minus_1 - $digit; | ||
| 443 | # } | ||
| 444 | # $n = ($n * $radix) + $digit; | ||
| 445 | # $xk ^= $digit; | ||
| 446 | # } | ||
| 447 | # { | ||
| 448 | # my $digit = pop @x; | ||
| 449 | # if ($xk & 1) { | ||
| 450 | # $digit = $radix_minus_1 - $digit; | ||
| 451 | # } | ||
| 452 | # $n = ($n * $radix) + $digit; | ||
| 453 | # $yk ^= $digit; | ||
| 454 | # } | ||
| 455 | # } | ||
| 456 | # } | ||
| 457 | # | ||
| 458 | # return $n; | ||
| 459 | |||
| 460 | # return $n; | ||
| 461 | } | ||
| 462 | |||
| 463 | # not exact | ||
| 464 | sub rect_to_n_range { | ||
| 465 | my ($self, $x1,$y1, $x2,$y2) = @_; | ||
| 466 | |||
| 467 | $x1 = round_nearest ($x1); | ||
| 468 | $y1 = round_nearest ($y1); | ||
| 469 | $x2 = round_nearest ($x2); | ||
| 470 | $y2 = round_nearest ($y2); | ||
| 471 | ($x1,$x2) = ($x2,$x1) if $x1 > $x2; | ||
| 472 | ($y1,$y2) = ($y2,$y1) if $y1 > $y2; | ||
| 473 | ### rect_to_n_range(): "$x1,$y1 to $x2,$y2" | ||
| 474 | |||
| 475 | if ($x2 < 0 || $y2 < 0) { | ||
| 476 | return (1, 0); | ||
| 477 | } | ||
| 478 | |||
| 479 | my $radix = $self->{'radix'}; | ||
| 480 | |||
| 481 | my ($pow, $level) = round_down_pow (max($x2,$y2), $radix); | ||
| 482 | if (is_infinite($level)) { | ||
| 483 | return (0, $level); | ||
| 484 | } | ||
| 485 | |||
| 486 | $pow *= $radix; | ||
| 487 | return (0, $pow*$pow - 1); | ||
| 488 | |||
| 489 | |||
| 490 | |||
| 491 | |||
| 492 | |||
| 493 | # my $n_power = $power * $power; | ||
| 494 | # my $max_x = 0; | ||
| 495 | # my $max_y = 0; | ||
| 496 | # my $max_n = 0; | ||
| 497 | # my $max_xk = 0; | ||
| 498 | # my $max_yk = 0; | ||
| 499 | # | ||
| 500 | # my $min_x = 0; | ||
| 501 | # my $min_y = 0; | ||
| 502 | # my $min_n = 0; | ||
| 503 | # my $min_xk = 0; | ||
| 504 | # my $min_yk = 0; | ||
| 505 | # | ||
| 506 | # my $serpentine_array = $self->{'serpentine_array'}; | ||
| 507 | # if ($serpentine_array->[0] && ($level&1)) { | ||
| 508 | # $max_xk = $max_yk = $min_xk = $min_yk = 1; | ||
| 509 | # } | ||
| 510 | # | ||
| 511 | # | ||
| 512 | # # l<=c<h doesn't overlap c1<=c<=c2 if | ||
| 513 | # # l>c2 or h-1<c1 | ||
| 514 | # # l>c2 or h<=c1 | ||
| 515 | # # so does overlap if | ||
| 516 | # # l<=c2 and h>c1 | ||
| 517 | # # | ||
| 518 | # my $radix_minus_1 = $radix - 1; | ||
| 519 | # my $overlap = sub { | ||
| 520 | # my ($c,$ck,$digit, $c1,$c2) = @_; | ||
| 521 | # if ($ck & 1) { | ||
| 522 | # $digit = $radix_minus_1 - $digit; | ||
| 523 | # } | ||
| 524 | # ### overlap consider: "inv@{[$ck&1]}digit=$digit ".($c+$digit*$power)."<=c<".($c+($digit+1)*$power)." cf $c1 to $c2 incl" | ||
| 525 | # return ($c + $digit*$power <= $c2 | ||
| 526 | # && $c + ($digit+1)*$power > $c1); | ||
| 527 | # }; | ||
| 528 | # | ||
| 529 | # while ($power > 1) { | ||
| 530 | # $power = int($power/$radix); | ||
| 531 | # $n_power = int($n_power/$radix); | ||
| 532 | # | ||
| 533 | # my $min_transpose = ($min_xk ^ $min_yk) & 1; | ||
| 534 | # my $max_transpose = ($min_xk ^ $min_yk) & 1; | ||
| 535 | # | ||
| 536 | # ### $power | ||
| 537 | # ### $n_power | ||
| 538 | # ### $max_n | ||
| 539 | # ### $min_n | ||
| 540 | # if ($max_transpose) { | ||
| 541 | # my $digit; | ||
| 542 | # for ($digit = $radix_minus_1; $digit > 0; $digit--) { | ||
| 543 | # last if &$overlap ($max_x,$max_xk,$digit, $x1,$x2); | ||
| 544 | # } | ||
| 545 | # $max_n += $n_power * $digit; | ||
| 546 | # $max_yk ^= $digit; | ||
| 547 | # if ($max_xk&1) { $digit = $radix_minus_1 - $digit; } | ||
| 548 | # $max_x += $power * $digit; | ||
| 549 | # } else { | ||
| 550 | # my $digit; | ||
| 551 | # for ($digit = $radix_minus_1; $digit > 0; $digit--) { | ||
| 552 | # last if &$overlap ($max_y,$max_yk,$digit, $y1,$y2); | ||
| 553 | # } | ||
| 554 | # $max_n += $n_power * $digit; | ||
| 555 | # $max_xk ^= $digit; | ||
| 556 | # if ($max_yk&1) { $digit = $radix_minus_1 - $digit; } | ||
| 557 | # $max_y += $power * $digit; | ||
| 558 | # } | ||
| 559 | # | ||
| 560 | # if ($min_transpose) { | ||
| 561 | # my $digit; | ||
| 562 | # for ($digit = 0; $digit < $radix_minus_1; $digit++) { | ||
| 563 | # last if &$overlap ($min_x,$min_xk,$digit, $x1,$x2); | ||
| 564 | # } | ||
| 565 | # $min_n += $n_power * $digit; | ||
| 566 | # $min_yk ^= $digit; | ||
| 567 | # if ($min_xk&1) { $digit = $radix_minus_1 - $digit; } | ||
| 568 | # $min_x += $power * $digit; | ||
| 569 | # } else { | ||
| 570 | # my $digit; | ||
| 571 | # for ($digit = 0; $digit < $radix_minus_1; $digit++) { | ||
| 572 | # last if &$overlap ($min_y,$min_yk,$digit, $y1,$y2); | ||
| 573 | # } | ||
| 574 | # $min_n += $n_power * $digit; | ||
| 575 | # $min_xk ^= $digit; | ||
| 576 | # if ($min_yk&1) { $digit = $radix_minus_1 - $digit; } | ||
| 577 | # $min_y += $power * $digit; | ||
| 578 | # } | ||
| 579 | # | ||
| 580 | # $n_power = int($n_power/$radix); | ||
| 581 | # if ($max_transpose) { | ||
| 582 | # my $digit; | ||
| 583 | # for ($digit = $radix_minus_1; $digit > 0; $digit--) { | ||
| 584 | # last if &$overlap ($max_y,$max_yk,$digit, $y1,$y2); | ||
| 585 | # } | ||
| 586 | # $max_n += $n_power * $digit; | ||
| 587 | # $max_xk ^= $digit; | ||
| 588 | # if ($max_yk&1) { $digit = $radix_minus_1 - $digit; } | ||
| 589 | # $max_y += $power * $digit; | ||
| 590 | # } else { | ||
| 591 | # my $digit; | ||
| 592 | # for ($digit = $radix_minus_1; $digit > 0; $digit--) { | ||
| 593 | # last if &$overlap ($max_x,$max_xk,$digit, $x1,$x2); | ||
| 594 | # } | ||
| 595 | # $max_n += $n_power * $digit; | ||
| 596 | # $max_yk ^= $digit; | ||
| 597 | # if ($max_xk&1) { $digit = $radix_minus_1 - $digit; } | ||
| 598 | # $max_x += $power * $digit; | ||
| 599 | # } | ||
| 600 | # | ||
| 601 | # if ($min_transpose) { | ||
| 602 | # my $digit; | ||
| 603 | # for ($digit = 0; $digit < $radix_minus_1; $digit++) { | ||
| 604 | # last if &$overlap ($min_y,$min_yk,$digit, $y1,$y2); | ||
| 605 | # } | ||
| 606 | # $min_n += $n_power * $digit; | ||
| 607 | # $min_xk ^= $digit; | ||
| 608 | # if ($min_yk&1) { $digit = $radix_minus_1 - $digit; } | ||
| 609 | # $min_y += $power * $digit; | ||
| 610 | # } else { | ||
| 611 | # my $digit; | ||
| 612 | # for ($digit = 0; $digit < $radix_minus_1; $digit++) { | ||
| 613 | # last if &$overlap ($min_x,$min_xk,$digit, $x1,$x2); | ||
| 614 | # } | ||
| 615 | # $min_n += $n_power * $digit; | ||
| 616 | # $min_yk ^= $digit; | ||
| 617 | # if ($min_xk&1) { $digit = $radix_minus_1 - $digit; } | ||
| 618 | # $min_x += $power * $digit; | ||
| 619 | # } | ||
| 620 | # } | ||
| 621 | # ### is: "$min_n at $min_x,$min_y to $max_n at $max_x,$max_y" | ||
| 622 | # return ($min_n, $max_n); | ||
| 623 | } | ||
| 624 | |||
| 625 | #------------------------------------------------------------------------------ | ||
| 626 | # levels | ||
| 627 | |||
| 628 | use Math::PlanePath::ZOrderCurve; | ||
| 629 | *level_to_n_range = \&Math::PlanePath::ZOrderCurve::level_to_n_range; | ||
| 630 | *n_to_level = \&Math::PlanePath::ZOrderCurve::n_to_level; | ||
| 631 | |||
| 632 | |||
| 633 | #----------------------------------------------------------------------------- | ||
| 634 | 1; | ||
| 635 | __END__ |