NAME

Math::PlanePath::AztecDiamondRings -- rings around an Aztec diamond shape

SYNOPSIS

use Math::PlanePath::AztecDiamondRings;
my $path = Math::PlanePath::AztecDiamondRings->new;
my ($x, $y) = $path->n_to_xy (123);

DESCRIPTION

This path makes rings around an Aztec diamond shape,

                    67  66                             5
                68  46  45  65                         4
            69  47  29  28  44  64                     3
        70  48  30  16  15  27  43  63                 2
    71  49  31  17   7   6  14  26  42  62             1
72  50  32  18   8   2   1   5  13  25  41  61     <- Y=0
73  51  33  19   9   3   4  12  24  40  60  84        -1
    74  52  34  20  10  11  23  39  59  83            -2
        75  53  35  21  22  38  58  82                -3
            76  54  36  37  57  81                    -4
                77  55  56  80                        -5
                    78  79                            -6

                         ^
-6  -5  -4  -3  -2  -1  X=0  1   2   3   4   5

This is very similar to the DiamondSpiral, but has all four corners flattened to 2 vertical or horizontal, instead of just one in the DiamondSpiral. This is only a small change to the alignment of numbers in the sides, but is more symmetric.

The hexagonal numbers 1,6,15,28,45,66,etc, k*(2k-1), are the vertical at X=0 going upwards. The hexagonal numbers of the "second kind" 3,10,21,36,55,78, etc k*(2k+1), are the vertical at X=-1 going downwards. Combining those two is the triangular numbers 3,6,10,15,21,etc, k*(k+1)/2, alternately on one line and the other.

FUNCTIONS

See "FUNCTIONS" in Math::PlanePath for behaviour common to all path classes.

$path = Math::PlanePath::AztecDiamondRings->new ()

Create and return a new Aztec diamond spiral object.

($x,$y) = $path->n_to_xy ($n)

Return the X,Y coordinates of point number $n on the path.

For $n < 1 the return is an empty list, it being considered the path starts at 1.

$n = $path->xy_to_n ($x,$y)

Return the point number for coordinates $x,$y. $x and $y are each rounded to the nearest integer, which has the effect of treating each point in the path as a square of side 1, so the entire plane is covered.

($n_lo, $n_hi) = $path->rect_to_n_range ($x1,$y1, $x2,$y2)

The returned range is exact, meaning $n_lo and $n_hi are the smallest and biggest in the rectangle.

FORMULAS

X,Y to N

The path makes lines in each quadrant. The quadrant is determined by the signs of X and Y, then count which line in that quadrant with d=X+Y or d=X-Y, then a quadratic in d for the starting N of the line in that quadrant.

Y>=0 X>=0   d=X+Y   N=(2d+2)*d+1 + Y
Y>=0 X<0    d=Y-X   N=2d^2       - Y
Y<0  X>=0   d=X-Y   N=(2d+2)*d+1 + Y
Y<0  X<0    d=X+Y   N=(2d+4)*d+2 - Y

For example

Y=2 X=3     d=2+3=5      N=(2*5+2)*5+1  + 2  = 63
Y=2 X=-1    d=2-(-1)=3   N=2*3*3        - 2  = 16
Y=-1 X=4    d=4-(-1)=5   N=(2*5+2)*5+1  + -1 = 60
Y=-2 X=-3   d=-3+(-2)=-5 N=(2*-5+4)*-5+2 - (-2) = 34

The two X>=0 cases are the same N formula and can be combined with an abs,

X>=0        d=X+abs(Y)   N=(2d+2)*d+1 + Y

This works because at Y=0 the last line of one ring joins up to the start of the next. For example N=11 to N=15,

15             2
  \ 
   14          1
     \
      13   <- Y=0
     /
   12         -1
  /
11            -2

 ^
X=0 1  2

Rectangle to N Range

Within each row N increases as X increases away from the Y axis, and within each column similarly N increases as Y increases away from the X axis. So in a rectangle the maximum N is at one of the four corners of the rectangle.

          |
x1,y2 M---|----M x2,y2
      |   |    |
   -------O---------
      |   |    |
      |   |    |
x1,y1 M---|----M x1,y1
          |

For any two rows y1 and y2, the values in row y2 are all bigger if y2>=-y1. This is so even when y1 and y2 are on the same side of the origin, ie. both positive or both negative.

For any two columns x1 and x2, the values in the part with Y>=0 are all bigger if x2>=-x1, or in the part of the columns with Y<0 it's x2>=-x1-1. So the biggest corner is at

max_y = (y2 >= -y1              ? y2 ? y1)
max_x = (x2 >= -x1 - (max_y<0)  ? x2 : x1)

The difference in the X handling for Y positive or negative is due to the quadrant ordering. When Y>=0 the bigger values are on the X negative side, but when Y<0 they're on the X positive side.

A similar approach gives the minimum in a rectangle.

if y2 < 0 then min_y=y2, and xbase=-1
if y1 > 0 then min_y=y1, and xbase=0
else           min_y=0,  and xbase=0

if x2 < xbase then min_x=x2
if x1 > xbase then min_x=x1
else               min_x=xbase

The minimum row is Y=0, but if that's not in the rectangle then the y2 or y1 top or bottom edge. Then within a row the minimum N is at xbase=0 if Y<0 or xbase=-1 if Y>=0. If that xbase is not in range then the x2 or x1 left or right edge.

SEE ALSO

Math::PlanePath, Math::PlanePath::DiamondSpiral

HOME PAGE

http://user42.tuxfamily.org/math-planepath/index.html

LICENSE

Copyright 2011, 2012 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/>.