The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

Math::PlanePath::Hypot -- points in order of hypotenuse distance

SYNOPSIS

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

DESCRIPTION

This path visits integer points X,Y in order of their distance from the origin 0,0, or anti-clockwise from the X axis among those of equal distance,

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

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

For example N=58 is at X=4,Y=-1 is sqrt(4*4+1*1) = sqrt(17) from the origin. The next furthest from the origin is X=3,Y=3 at sqrt(18).

In general the X,Y points are the sums of two squares X^2+Y^2 taken in increasing order of that hypotenuse, with negative X or Y and swapped Y,X included.

Equal Distances

Points with the same distance are taken in anti-clockwise order around from the X axis. For example X=3,Y=1 is sqrt(10) from the origin, as are the swapped X=1,Y=3, and negative X=-1,Y=3 etc in other quadrants, for a total 8 points N=30 to N=37 all the same distance.

When one of X or Y is 0 there's no negative, so just four negations like N=10 to 13 points X=2,Y=0 through X=0,Y=-2. Or on the diagonal X==Y there's no swap, so just four like N=22 to N=25 points X=3,Y=3 through X=3,Y=-3.

There can be more than one way for the same distance to arise. A Pythagorean triple like 3^2 + 4^2 == 5^2 has 8 points from the 3,4 plus 4 points from the 5,0 giving a total 12 points N=70 to N=81. Other combinations like 20^2 + 15^2 == 24^2 + 7^2 occur, and with more than two different ways to have the same sum too.

Multiples of 4

The first point of a given distance from the origin is either on the X axis or somewhere in the first octant. The row Y=1 just above the axis is always first from X>=2 onwards, and similarly further rows for big enough X.

Since there's always a multiple of 4 many points with the same distance, the first point has N=4*k+2, and similarly on the negative X side N=4*j. If you plot the prime numbers on the path then those even number N's (composites) are just above the X axis, and on and just below the negative X axis.

Circle Lattice

Gauss's circle lattice problem asks how many integer X,Y points there are within a circle of radius R.

The points on the X axis N=2,10,26,46, etc are the first for which X^2+Y^2==R^2 (integer X==R), so N-1 there is the number of points strictly inside, ie. X^2+Y^2 < R^2 (Sloane's A051132 http://oeis.org/A051132).

The last point satisfying X^2+Y^2==R^2 is either in the octant just below the X axis, or is on the negative Y axis. Those N's are the number of points X^2+Y^2<=R^2, Sloane's A000328.

When that A000328 is plotted on the path a straight line can be seen in the fourth quadrant extending down just above the diagonal. It arises from multiples of the Pythagorean 3^2 + 4^2, first X=4,Y=-3, then X=8,Y=-6, etc X=4*k,Y=-3*k. Sometimes the multiple is not the last among those of that 5*k radius though, so there's gaps in the line. For example 20,-15 is not the last since 24,-7 is also 25 away from the origin.

FUNCTIONS

$path = Math::PlanePath::Hypot->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, it being considered 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.

FORMULAS

The calculations are not particularly efficient currently. Private arrays are built similar to what's described for HypotOctant, but with replication for negative and swapped X,Y.

SEE ALSO

Math::PlanePath, Math::PlanePath::HypotOctant, Math::PlanePath::PixelRings, Math::PlanePath::PythagoreanTree

HOME PAGE

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

LICENSE

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