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 duplicated turned +90 degrees left as N=1 to N=2. Then N=0to2 is duplicated likewise turned +90 degrees to make N=2to4. And so on doubling.
The 90 degree rotation is relative to the initial N=0to1 direction. So N=0to1 is left, so at N=2^level the first segment is upwards +90.
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. There are points with an arbitrary number of repetitions (is that right?). The repetitions are always finite, the curve doesn't turn back on itself endlessly, only a limited extent.
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 returnundef
. $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.
For powers-of-2 N=2,4,8,16, etc, there's just one 1 bit so the direction is always +90 degrees, ie. upwards, at those points.
Turn Sequence
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 in that case, 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 1s,
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 you have at N will be 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]
If N is fractional then the direction at int(N) and the turn at int(N)+1 are combined. The two are similar calculations. The direction is the count of all 1-bits, the turn is the count of low 1-bits, ie. those up to the lowest 0.
# 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 swapping and a 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)
A179868 - direction 0to3, being count of 1 bits mod 4
A000120 - direction as total turn, count of 1 bits
A007814 - count low 0s, is turn by value-1 to the right
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/>.