NAME

Math::PlanePath::TriangularHypot -- points of triangular lattice in order of hypotenuse distance

SYNOPSIS

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

DESCRIPTION

This path visits X,Y points on a triangular "A2" lattice in order of their distance from the origin 0,0 and anti-clockwise around from the X axis among those of equal distance.

         58    47    39    46    57                 4

      48    34    23    22    33    45              3

   40    24    16     9    15    21    38           2

49    25    10     4     3     8    20    44        1

   35    17     5     1     2    14    32      <- Y=0

50    26    11     6     7    13    31    55       -1

   41    27    18    12    19    30    43          -2

      51    36    28    29    37    54             -3

         60    52    42    53    61                -4

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

The lattice is put on a square X,Y grid using every second point per "Triangular Lattice" in Math::PlanePath. With scaling X/2, Y*sqrt(3)/2 to give equilateral triangles with side length 1 the X,Y distance from the origin is

dist^2 = (X/2^2 + (Y*sqrt(3)/2)^2  =  (X^2 + 3*Y^2) / 4

For example N=19 at X=2,Y=-2 is sqrt((2**2+3*-2**2)/4) = sqrt(4) from the origin. The next smallest after that is X=5,Y=1 at sqrt(7). The key part is X^2 + 3*Y^2 as the distance measure to order the points.

Equal Distances

Points with the same distance are taken in anti-clockwise order around from the X axis. For example N=14 at X=4,Y=0 is sqrt(4) from the origin, as are the rotated X=2,Y=2 and X=--2,Y=2 etc in other sixths, for a total 6 points N=14 to N=19 all the same distance.

In general there's either 6 or 12 symmetric points so the number of points of the same distance is always a multiple of 6 or 12. There are 6 points when on the six radial lines X=0, X=Y or X=-Y, and the lines Y=0, X=3*Y or X=-3*Y which are midway between then. Then there's 12-way symmetry for anything else, ie. anything in the twelve slices between those twelve lines. The first 12 for example is N=20 to N=31 all at sqrt(28).

There can also be multiple ways for the same distance to arise, but the 6-way or 12-way symmetry means always a multiple of 6 or 12.

FUNCTIONS

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

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

Create and return a new hypot path 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 as the first point at X=0,Y=0 is N=1.

Currently it's unspecified what happens if $n is not an integer. Successive points are a fair way apart, so it may not make much sense to say give an X,Y position in between the integer $n.

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

Return an integer point number for coordinates $x,$y. Each integer N is considered the centre of a unit square and an $x,$y within that square returns N.

Only every second square in the plane has an N, being those where X,Y both odd or both even. If $x,$y is a position without an N, ie. one of X,Y odd the other even, then the return is undef.

OEIS

Entries in Sloane's Online Encyclopedia of Integer Sequences related to this path include,

http://oeis.org/A035019

A004016 -- number of points of rsquared distance h
A035019 -- number of points of equal hypot distance
            (excludes distances with 0 points)

A004016 is based on x^2+x*y+y^2 = h. That x,y is obtained from the path X,Y here by a rotation -45 degrees,

x = (Y-X)/2           X = 2*(x+y)
y = (X+Y)/2           Y = 2*(y-x)

The target h is then

h = x^2+x*y+y^2
  = ((Y-X)/2)^2 + (Y-X)/2 * (X+Y)/2 + ((X+Y)/2)^2
  = (X^2 + 3*Y^2) / 4

which is the dist^2 distance in equilateral triangles of unit side as described above.

SEE ALSO

Math::PlanePath, Math::PlanePath::Hypot, Math::PlanePath::HypotOctant, Math::PlanePath::PixelRings, Math::PlanePath::HexSpiral

HOME PAGE

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

LICENSE

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