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

NAME

Math::PlanePath::CCurve -- Levy C curve

SYNOPSIS

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

DESCRIPTION

This is an integer version of the "C" curve.

                          11-----10-----9,7-----6------5               3
                           |             |             |
                   13-----12             8             4------3        2
                    |                                         |
            19---14,18----17                                  2        1
             |      |      |                                  |
     21-----20     15-----16                           0------1   <- Y=0
      |
     22                                                               -1
      |
    25,23---24                                                        -2
      |
     26     35-----34-----33                                          -3
      |      |             |
    27,37--28,36          32                                          -4
      |      |             |
     38     29-----30-----31                                          -5
      |
    39,41---40                                                        -6
      |
     42                                              ...              -7
      |                                                |
     43-----44     49-----48                          64-----63       -8
             |      |      |                                  |
            45---46,50----47                                 62       -9
                    |                                         |
                   51-----52            56            60-----61      -10
                           |             |             |
                          53-----54----55,57---58-----59             -11

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

The initial segment N=0 to N=1 is repeated with a turn +90 degrees left to give N=1 to N=2. Then N=0to2 is repeated likewise turned +90 degrees to make N=2to4. And so on doubling each time.

The 90 degree rotation is relative to the initial N=0to1 direction. So at N=0 the direction is left, and the repeat at N=2^level is turned by +90 making it upwards at N=1,2,4,8,16,etc.

The curve crosses itself and repeats some X,Y positions. The first doubled point is X=-2,Y=3 which is both N=7 and N=9. The first tripled point is X=18,Y=-7 which is N=189, N=279 and N=281. The number of repeats at a given point is finite, but as N increases there's points where that number of repeats becomes ever bigger (is that right?).

FUNCTIONS

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

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

Create and return a new path object.

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

Return the X,Y coordinates of point number $n on the path. Points begin at 0 and if $n < 0 then the return is an empty list.

Fractional positions give an X,Y position along a straight line between the integer positions.

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

Return the point number for coordinates $x,$y. If there's nothing at $x,$y then return undef.

$n = $path->n_start()

Return 0, the first N in the path.

FORMULAS

Direction

The direction or cumulative net turn of the curve is the count of 1 bits in N,

    direction = count_1_bits(N) * 90degrees

For example N=11 is binary 1011 has three 1 bits, so direction 3*90=270 degrees, ie. to the south.

This bit count is because at each power-of-2 position the curve is a copy of the lower bits but turned +90 degrees, so +90 for each 1-bit.

For powers-of-2 N=2,4,8,16, etc, there's only one 1 bit so the direction is always +90 degrees there, ie. upwards.

Turn

At each point N the curve can turn in any direction: left, right, straight, or 180 degrees back. The turn is given by number of low 0-bits of N,

    turn right = (count_low_0_bits(N) - 1) * 90degrees

For example N=8 is binary 0b100 which is 2 low zero bit for turn=(2-1)*90=90 degrees to the right.

When N is odd there's no low zero bits and the turn is always (0-1)*90=-90 to the right there, which means +90 turn to the left.

Next Turn

The turn at the point following N, ie. at N+1, can be calculated from the bits of N similarly, by counting the low 1-bits,

    following turn right = (count_low_1_bits(N) - 1) * 90degrees

For example N=11 is binary 0b1011 which is 2 low one bits for nextturn=(2-1)*90=90 degrees to the right at the following point, ie. at N=12.

This works simply because low ones like ..0111 increment to low zeros ..1000. The low ones at N are the low zeros at N+1.

N to dX,dY

n_to_dxdy() is implemented using the direction described above. If N is an integer then direction = count_1_bits mod 4 gives the direction for dX,dY.

    dir = count_1_bits(N) mod 4
    dx = dir_to_dx[dir]    # table 0 to 3
    dy = dir_to_dy[dir]

For fractional N then the direction at int(N) can be modified by the turn at int(N)+1 to give the direction at int(N)+1.

    # apply turn to make direction at Nint+1
    turn = count_low_1_bits(N) - 1      # N integer part
    dir = (dir - turn) mod 4            # direction at N+1

    # adjust dx,dy by fractional amount in this direction
    dx += Nfrac * (dir_to_dx[dir] - dx)
    dy += Nfrac * (dir_to_dy[dir] - dy)

A tiny optimization can be made by working the "-1" from the turn formula into a +90 degree rotation of the "dir_to_dx" and "dir_to_dy" parts by a swap and sign change,

    turn_plus_1 = count_low_1_bits(N)     # N integer part
    dir = (dir - turn_plus_1) mod 4       # direction-1 at N+1

    # adjustment including extra +90 degrees on dir
    dx -= $n*(dir_to_dy[dir] + dx)
    dy += $n*(dir_to_dx[dir] - dy)

X,Y to N

The N values at a given X,Y can be found by traversing the curve. At a given digit position if X,Y is within the curve extents at that level and position then descend to consider the next lower digit position, otherwise step to the next digit at the current digit position.

It's convenient to consider base-4 digits since that keeps the digit steps straight rather than diagonals. The maximum extent of the curve at a given even numbered level is

    k = level/2
    Lmax(level) = 2^k + int(2^(k-1) - 1);

For example k=2 is level=4, N=0 to N=2^4=16 has extent Lmax=2^2+2^1-1=5. That extent can be seen at points N=13,N=14,N=15.

The extents width ways or backwards are shorter and using them would tighten the traversal, cutting off some unnecessary descending. But the calculations are then a little trickier.

The first N found by this traversal is the smallest. Continuing the search gives all the N which are the target X,Y.

OEIS

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

    http://oeis.org/A179868  (etc)

    A010059 - abs(dX), count 1-bits mod 2
    A010060 - abs(dY), [count 1-bits + 1] mod 2, Thue-Morse

    A179868 - direction 0to3, count 1-bits mod 4
    A000120 - direction as total turn, count 1-bits

    A007814 - a(n)=turn-1 to the right, being count low 0s

    A003159 - N positions of left or right turn, ends even num 0 bits
    A036554 - N positions of straight or 180 turn, ends odd num 0 bits

SEE ALSO

Math::PlanePath, Math::PlanePath::DragonCurve, Math::PlanePath::AlternatePaper, Math::PlanePath::KochCurve

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