/usr/share/perl5/Math/PlanePath/KnightSpiral.pm is in libmath-planepath-perl 117-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 | # Copyright 2010, 2011, 2012, 2013, 2014 Kevin Ryde
# This file is part of Math-PlanePath.
#
# Math-PlanePath is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by the
# Free Software Foundation; either version 3, or (at your option) any later
# version.
#
# Math-PlanePath is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
# or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
# for more details.
#
# You should have received a copy of the GNU General Public License along
# with Math-PlanePath. If not, see <http://www.gnu.org/licenses/>.
package Math::PlanePath::KnightSpiral;
use 5.004;
use strict;
#use List::Util 'max';
*max = \&Math::PlanePath::_max;
use vars '$VERSION', '@ISA';
$VERSION = 117;
use Math::PlanePath;
@ISA = ('Math::PlanePath');
use Math::PlanePath::Base::Generic
'round_nearest';
# uncomment this to run the ### lines
#use Smart::Comments;
use constant xy_is_visited => 1;
sub x_negative_at_n {
my ($self) = @_;
return $self->n_start + 3;
}
sub y_negative_at_n {
my ($self) = @_;
return $self->n_start + 1;
}
sub _UNDOCUMENTED__dxdy_list_at_n {
my ($self) = @_;
return $self->n_start + 8;
}
use constant dx_minimum => -2;
use constant dx_maximum => 2;
use constant dy_minimum => -2;
use constant dy_maximum => 2;
use constant _UNDOCUMENTED__dxdy_list => (2,1, # ENE
1,2, # NNE
-1,2, # NNW
-2,1, # WNW
-2,-1, # WSW
-1,-2, # SSW
1,-2, # SSE
2,-1, # ESE
);
use constant absdx_minimum => 1;
use constant absdy_minimum => 1;
use constant dsumxy_minimum => -3; # -2,-1
use constant dsumxy_maximum => 3; # +2,+1
use constant ddiffxy_minimum => -3;
use constant ddiffxy_maximum => 3;
use constant dir_minimum_dxdy => (2,1); # X=2,Y=1 angle
use constant dir_maximum_dxdy => (2,-1);
# Maybe ...
# use constant parameter_info_array =>
# [
# Math::PlanePath::Base::Generic::parameter_info_nstart1(),
# ];
#------------------------------------------------------------------------------
sub new {
my $self = shift->SUPER::new(@_);
if (! defined $self->{'n_start'}) {
$self->{'n_start'} = $self->default_n_start;
}
return $self;
}
sub _odd {
my ($n) = @_;
### _odd(): $n
$n -= 2*int($n/2);
### rem: "$n"
if ($n > 1) {
return 2-$n;
} else {
return $n;
}
# return (int($n) % 2);
}
sub n_to_xy {
my ($self, $n) = @_;
#### KnightSpiral n_to_xy: $n
# adjust to N=1 at origin X=0,Y=0
$n = $n - $self->{'n_start'} + 1;
if ($n < 2) {
if ($n < 1) { return; }
$n--;
return (2*$n, -$n);
}
my $d = int ((7 + sqrt(int($n) - 1)) / 4);
my $d1 = $d-1;
my $outer = 2*$d1;
my $inner = $outer - 1;
my $p = 2*$d1;
my $p1 = $p - 1;
# use Smart::Comments;
#### s frac: .25 * (7 + sqrt($n - 1))
#### $d
#### $d1
#### $inner
#### $outer
#### $p
#### $p1
$n -= $d*(16*$d - 56) + 50;
#### remainder: $n
# one
#
if ($n < $p1) {
#### right upwards, eg 2 ...
return (- _odd($n) + $outer,
2*$n - $inner);
}
$n -= $p1;
if ($n < $p1) {
#### top leftwards, eg 3 ...
return (-2*$n + $inner,
_odd($n) + $inner);
}
$n -= $p1;
if ($n < $p) {
#### left downwards ...
return ( - _odd($n) - $inner,
-2*$n + $outer);
}
$n -= $p;
if ($n < $p1) {
#### bottom rightwards: $n
return (2*$n - $inner,
_odd($n) - $outer);
}
$n -= $p1;
### two ...
#
if ($n < $p1) {
### right upwards ...
return (_odd($n) + $inner,
2*$n - $inner);
}
$n -= $p1;
if ($n < $p) {
#### top leftwards
return (-2*$n + $outer,
_odd($n) + $inner);
}
$n -= $p;
if ($n < $p1) {
#### left downwards
return (_odd($n) - $outer,
-2*$n + $inner);
}
$n -= $p1;
if ($n < $p1) {
#### bottom rightwards: $n
return (2*$n - $inner,
- _odd($n) - $inner);
}
$n -= $p1;
### three ...
#
if ($n < $p) {
### right upwards, eg 12 ...
return (_odd($n) + $inner,
2*$n - $outer);
}
$n -= $p;
if ($n < $p1) {
### top leftwards, eg 14 ...
return (-2*$n + $inner,
- _odd($n) + $outer);
}
$n -= $p1;
if ($n < $p1) {
### left downwards, eg 15 ...
return (- _odd($n) - $inner,
-2*$n + $inner);
}
$n -= $p1;
if ($n < $p1) {
### bottom rightwards, eg 16 ...
return (2*$n - $outer,
- _odd($n) - $inner);
}
$n -= $p1;
### four ...
#
if ($n <= 1) {
### special 17 upwards ...
return ($n + $outer - 2,
2*$n - $outer);
}
if ($n < $p) {
### right upwards ...
return (- _odd($n) + $outer,
2*$n - $outer);
}
$n -= $p;
if ($n < $p) {
### top leftwards, eg 19 ...
return (-2*$n + $outer,
- _odd($n) + $outer);
}
$n -= $p;
if ($n < $p) {
### left downwards, eg 21 ...
return (_odd($n) - $outer,
-2*$n + $outer);
}
$n -= $p;
if ($n < $p) {
### bottom rightwards, eg 23 ...
return (2*$n - $outer,
_odd($n) - $outer);
}
$n -= $p;
### step outwards, eg 25 ...
return (2*$n + $outer,
- _odd($n) - $outer);
}
# 157 92 113 134 155 90 111 132 153 88 109 130 151
# 114 135 156 91 112 133 154 89 110 131 152 87 108
# 93 158 73 32 45 58 71 30 43 56 69 150 129
# 136 115 46 59 72 31 44 57 70 29 42 107 86
# 159 94 33 74 21 4 9 14 19 68 55 128 149
# 116 137 60 47 10 15 20 3 8 41 28 85 106
# 95 160| 75 34 | 5 22 1 18 13 | 54 67| 148 127
# 138 117 48 61 16 11 24 7 2 27 40 105 84
# 161 96 35 76 23 6 17 12 25 66 53 126 147
# 118 139 62 49 78 37 64 51 80 39 26 83 104
# 97 162 77 36 63 50 79 38 65 52 81 146 125
# 140 119 164 99 142 121 166 101 144 123 168 103 82
# 163 98 141 120 165 100 143 122 167 102 145 124 169
sub xy_to_n {
my ($self, $x, $y) = @_;
$x = round_nearest ($x);
$y = round_nearest ($y);
if ($x == 0 && $y == 0) {
return $self->{'n_start'};
}
my $r = max(abs($x),abs($y));
my $d = int (($r+1)/2); # ring number, counting $x=1,2 as $d==1
$r -= (~$r & 1); # next lower odd number
### $d
### $r
if ($y >= $r) {
### top horizontal
my $xodd = ($x & 1);
$x = ($x - $xodd) / 2;
### $xodd
### $x
# x odd
# [3,30,89,180,303] (16*$d**2 + -21*$d + 8)
# [14,57,132,239,378,549] (16*$d**2 + -5*$d + 3)
#
# [9,44,111,210,341,504] (16*$d**2 + -13*$d + 6)
# [20,71,154,269,416] (16*$d**2 + 3*$d + 1)
my $n = 16*$d*$d - $x;
if (($x ^ $y ^ $d) & 1) {
if ($xodd) {
return $n -5*$d + 2 + $self->{'n_start'};
} else {
return $n -13*$d + 5 + $self->{'n_start'};
}
} else {
if ($xodd) {
return $n -21*$d + 7 + $self->{'n_start'};
} else {
return $n + 3*$d + $self->{'n_start'};
}
}
}
# the lower left outer corner 25,81,169,etc belongs on the bottom
# horizontal, it's not an extension downwards from the right vertical
# (positions N=18,66,146,etc), hence $x!=-$y
#
if ($x >= $r && $x != -$y) {
### right vertical
my $yodd = ($y & 1);
$y = ($y - $yodd) / 2;
### $yodd
### $y
# y odd
# [3, 28,85, 174,295, 448,633] (16*$d**2 + -23*$d + 10)
# [8,41, 106,203, 332,493] (16*$d**2 + -15*$d + 7)
#
# y even
# [13,54,127,232,369,538] (16*$d**2 + -7*$d + 4)
# [18,67,148,261,406,583,792] (16*$d**2 + $d + 1)
#
my $n = 16*$d*$d + $y;
if (($x ^ $y ^ $d) & 1) {
if ($yodd) {
return $n -15*$d + 6 + $self->{'n_start'};
} else {
return $n -7*$d + 3 + $self->{'n_start'};
}
} else {
if ($yodd) {
return $n -23*$d + 9 + $self->{'n_start'};
} else {
return $n + $d + $self->{'n_start'};
}
}
}
if ($y <= -$r) {
### bottom horizontal
my $xodd = ($x & 1);
$x = ($x - $xodd) / 2;
### $xodd
### $x
# x odd
# [7,38,101,196,323] (16*$d**2 + -17*$d + 8)
# [12,51,122,225,360,527] (16*$d**2 + -9*$d + 5)
#
# x even
# [17,64,143,254,397,572] (16*$d**2 + -1*$d + 2)
# [24,79,166,285,436] (16*$d**2 + 7*$d + 1)
my $n = 16*$d*$d + $x;
if (($x ^ $y ^ $d) & 1) {
if ($xodd) {
return $n -9*$d + 4 + $self->{'n_start'};
} else {
return $n -1*$d + 1 + $self->{'n_start'};
}
} else {
if ($xodd) {
return $n -17*$d + 7 + $self->{'n_start'};
} else {
return $n + 7*$d + $self->{'n_start'};
}
}
}
if ($x <= -$r) {
### left vertical
my $yodd = ($y & 1);
$y = ($y - $yodd) / 2;
### $yodd
### $y
# y odd
# [10,47,116,217,350,515] (16*$d**2 + -11*$d + 5)
# [15,60,137,246,387] (16*$d**2 + -3*$d + 2)
#
# y even
# [5,34,95,188,313] (16*$d**2 + -19*$d + 8)
# [22,75,160,277,426] (16*$d**2 + 5*$d + 1)
#
my $n = 16*$d*$d - $y;
if (($x ^ $y ^ $d) & 1) {
if ($yodd) {
return $n -11*$d + 4 + $self->{'n_start'};
} else {
return $n -19*$d + 7 + $self->{'n_start'};
}
} else {
if ($yodd) {
return $n -3*$d + 1 + $self->{'n_start'};
} else {
return $n + 5*$d + $self->{'n_start'};
}
}
}
}
# not exact
sub rect_to_n_range {
my ($self, $x1,$y1, $x2,$y2) = @_;
$x1 = round_nearest ($x1);
$y1 = round_nearest ($y1);
$x2 = round_nearest ($x2);
$y2 = round_nearest ($y2);
my $x = max(abs($x1),abs($x2));
my $y = max(abs($y1),abs($y2));
my $d = max(abs($x),abs($y));
$d += ($d & 1); # next even number if not already even
### $x
### $y
### $d
### is: $d*$d
$d = 2*$d+1; # width of whole square
# ENHANCE-ME: find actual minimum if rect doesn't cover 0,0
return ($self->{'n_start'},
$self->{'n_start'} + $d*$d);
}
1;
__END__
=for stopwords versa Ryde Math-PlanePath OEIS
=head1 NAME
Math::PlanePath::KnightSpiral -- integer points around a square, by chess knight moves
=head1 SYNOPSIS
use Math::PlanePath::KnightSpiral;
my $path = Math::PlanePath::KnightSpiral->new;
my ($x, $y) = $path->n_to_xy (123);
=head1 DESCRIPTION
This path traverses the plane by an infinite "knight's tour" in the form
of a square spiral.
...
21 4 9 14 19 2
10 15 20 3 8 28 1
5 22 1 18 13 <- Y=0
16 11 24 7 2 27 1
23 6 17 12 25 2
26
^
-2 -1 X=0 1 2 3
Each step is a chess knight's move 1 across and 2 along, or vice versa. The
pattern makes 4 cycles on a 2-wide path around a square before stepping
outwards to do the same again to a now bigger square. The above sample
shows the first 4-cycle around the central 1, then stepping out at 26 and
beginning to go around the outside of the 5x5 square.
An attractive traced out picture of the path can be seen at the following
page (quarter way down under "Open Knight's Tour"),
=over
L<http://www.borderschess.org/KTart.htm>
L<http://www.borderschess.org/KTinfinity.gif>
L<http://www.borderschess.org/Infinite.gif>
=back
See L<math-image> to draw the path lines too.
=head1 FUNCTIONS
See L<Math::PlanePath/FUNCTIONS> for behaviour common to all path classes.
=over 4
=item C<$path = Math::PlanePath::KnightSpiral-E<gt>new ()>
Create and return a new knight spiral object.
=item C<($x,$y) = $path-E<gt>n_to_xy ($n)>
Return the X,Y coordinates of point number C<$n> on the path.
For C<$n < 1> the return is an empty list, it being considered the path
starts at 1.
=item C<$n = $path-E<gt>xy_to_n ($x,$y)>
Return the point number for coordinates C<$x,$y>. C<$x> and C<$y> are
each rounded to the nearest integer, which has the effect of treating each N
in the path as centred in a square of side 1, so the entire plane is
covered.
=back
=head1 OEIS
This Knight's tour is in Sloane's OEIS following the Knight spiral and
giving the resulting X,Y location by the C<SquareSpiral> numbering. There's
eight forms for 4 rotations and spiralling the same or opposite directions.
=over
L<http://oeis.org/A068608> (etc)
=back
permutations
A068608 same knight and square spiral directions
A068609 rotate 90 degrees
A068610 rotate 180 degrees
A068611 rotate 270 degrees
A068612 rotate 180 degrees, spiral opp dir (X negate)
A068613 rotate 270 degrees, spiral opp dir
A068614 spiral opposite direction (Y negate)
A068615 rotate 90 degrees, spiral opp dir (X,Y transpose)
See F<examples/knights-oeis.pl> for a sample program printing the values of
A068608.
=head1 SEE ALSO
L<Math::PlanePath>,
L<Math::PlanePath::SquareSpiral>
=head1 HOME PAGE
L<http://user42.tuxfamily.org/math-planepath/index.html>
=head1 LICENSE
Copyright 2010, 2011, 2012, 2013, 2014 Kevin Ryde
This file is part of Math-PlanePath.
Math-PlanePath is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the Free
Software Foundation; either version 3, or (at your option) any later
version.
Math-PlanePath is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
more details.
You should have received a copy of the GNU General Public License along with
Math-PlanePath. If not, see <http://www.gnu.org/licenses/>.
=cut
|