Kevin Ryde > Math-PlanePath-110 > Math::PlanePath::CCurve

Math-PlanePath-110.tar.gz

Dependencies

Annotate this POD

Website

# CPAN RT

 Open 2
View/Report Bugs
Module Version: 110   Source   Latest Release: Math-PlanePath-114

# 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 Levy "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 always relative to the initial N=0to1 direction along the X axis. So at any N=2^level the turn is +90 making the direction upwards at each of 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 always 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 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 a single 1-bit so the direction is always +90 degrees there, ie. always 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 the 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 0-bits 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 in that case, which means every second turn is 90 degrees to the left.

## Next Turn

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

`    next 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 1-bits like ..0111 increment to low 0-bits ..1000 to become N+1. The low 1-bits at N are thus the low 0-bits at N+1.

## N to dX,dY

`n_to_dxdy()` is implemented using the direction described above. If N is an integer then count 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 the direction at int(N)+1 can be obtained from the direction at int(N) by applying the turn at int(N)+1, that being the low 1-bits of N per "Next Turn" above. Those two directions can then be combined per "N to dX,dY -- Fractional" in Math::PlanePath.

```    # 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" of 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)     # on 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 work in 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 + floor(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 and 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

```    A010059   abs(dX), count1bits(N) mod 2
A010060   abs(dY), count1bits(N)+1 mod 2, being Thue-Morse

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

A096268   turn 1=straight,0=left or right
A007814   turn-1 to the right, being count low 0-bits

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

A146559   X at N=2^k, being Re((i+1)^k)
A009545   Y at N=2^k, being Im((i+1)^k)```

ccurve(6x) back end of xscreensaver(1) displaying the C curve (and various other dragon curve and Koch curves).

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