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

NAME

Math::PlanePath::DragonCurve -- dragon curve

SYNOPSIS

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

DESCRIPTION

This is the dragon or paper folding curve by Heighway, Harter, et al,

                 9----8    5---4               2
                 |    |    |   |
                10--11,7---6   3---2           1
                      |            |
      17---16   13---12        0---1       <- Y=0
       |    |    |
      18-19,15-14,22-23                       -1
            |    |    |
           20--21,25-24                       -2
                 |
                26---27                       -3
                      |
    --32   29---29---28                       -4
       |    |
      31---30                                 -5

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

The curve visits "inside" X,Y points twice. The first of these is X=-2,Y=1 which is N=7 and also N=11. The segments N=6,7,8 and N=10,11,12 have touched, but the path doesn't cross itself. The doubled vertices are all like this, touching but not crossing, and no edges repeating.

The first step N=1 is to the right along the X axis and the path then slowly spirals counter-clockwise and progressively fatter. The end of each replication is N=2^level which is at level*45 degrees around,

    N       X,Y     angle
   ----    -----    -----
     1      1,0        0
     2      1,1       45
     4      0,2       90
     8     -2,2      135
    16     -4,0      180
    32     -4,-4     225
   ...

Here's points N=0 to N=2^9=512. "0" is the origin and "+" is N=512. Notice it's spiralled around full-circle to angle 45 degrees up again, like the initial N=2.

                                    * *     * *
                                  * * *   * * *
                                  * * * * * * * * *
                                  * * * * * * * * *
                            * *   * * * *       * *
                          * * *   * * * *     + * *
                          * * * * * *         * *
                          * * * * * * *
                          * * * * * * * *
                              * * * * * *
                              * * * *
                                  * * * * * * *
                            * *   * * * * * * * *
                          * * *   * * * * * * * *
                          * * * * * * * * * *
                          * * * * * * * * * * * * * * *
                          * * * * * * * * * * * * * * * *
                              * * * * * * * * * * * * * *
                              * * * * * * * * * * * *
        * * * *                   * * * * * * * * * * *
        * * * * *           * *   * * * *       * * * * *
    * * * *   0 *         * * *   * * * *   * * * * * * *
    * * * *               * * * * * *       * * * * *
      * * *               * * * * * * *       * * * *
        * * * *     * *   * * * * * * * *
    * * * * * *   * * *   * * * * * * * *
    * * * * * * * * * * * * * * * * *
      * * * * * * * * * * * * * * * * *
                * * * * *       * * * * *
            * * * * * * *   * * * * * * *
            * * * * *       * * * * *
              * * * *         * * * *

At a power of two N=2^level for N=2 or higher, the curve always goes upward to that point, then leaves it to the left. For example at N=16 the curve goes up from N=15 to N=16, then goes left for N=16 to N=17. Likewise at N=32, etc. So the spiral is curling around ever further, but the self-similar twist back again means the N=2^level endpoint is always at the same up/left orientation. (See "Total Turn" below for the net direction in general.)

Arms

The curve fills a quarter of the plane and four copies mesh together perfectly when rotated by 90, 180 and 270 degrees. The arms parameter can choose 1 to 4 curve arms, successively advancing.

For example arms => 4 begins as follows, with N=0,4,8,12,etc being one arm, N=1,5,9,13 the second, N=2,6,10,14 the third and N=3,7,11,15 the fourth.

             20 ------ 16
                        |
              9 ------5/12 -----  8       23
              |         |         |        |
     17 --- 13/6 --- 0/1/2/3 --- 4/15 --- 19
      |       |         |         |
     21      10 ----- 14/7 ----- 11
                        |
                       18 ------ 22

With four arms every X,Y point is visited twice (except the origin 0,0 where all four begin) and every edge between the points is traversed once.

Paper Folding

The path is called a paper folding curve because it can be generated by thinking of a long strip of paper folded in half repeatedly then unfolded so each crease is a 90 degree angle. The effect is that the curve repeats in successive doublings turned by 90 degrees and reversed.

The first segment unfolds, pivoting at the "1",

                                          2
                                     ->   |
                     unfold         /     |
                      ===>         |      |
                                          |
    0-------1                     0-------1

Then the same again with that L shape, pivoting at the "2", then next pivoting at the "4", and so on.

                                 4
                                 |
                                 |
                                 |
                                 3--------2
           2                              |
           |        unfold          ^     |
           |         ===>            \_   |
           |                              |
    0------1                     0--------1

It can be shown that this unfolding doesn't overlap itself but the corners may touch, such as at the X=-2,Y=1 etc noted above.

FUNCTIONS

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

$path = Math::PlanePath::DragonCurve->new ()
$path = Math::PlanePath::DragonCurve->new (arms => 4)

Create and return a new path object.

The optional arms parameter can make 1 to 4 copies of the curve, each arm successively advancing.

($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.

The curve visits an $x,$y twice for various points (all the "inside" points). In the current code the smaller of the two N values is returned. Is that the best way?

@n_list = $path->xy_to_n_list ($x,$y)

Return a list of N point numbers for coordinates $x,$y. There can be none, one or two N's for a given $x,$y.

$n = $path->n_start()

Return 0, the first N in the path.

FORMULAS

X,Y to N

In the current implementation the four edges around a point are converted to DragonMidpoint by a rotate -45 and offset. This gives four candidate N values and those which converts back to the desired X,Y by n_to_xy() are the results for xy_to_n_list().

    Xmid,Ymid = X+Y, Y-X    # rotate -45 degrees
    dx = 0 or -1
      dy = 0 or 1
        N candidate = DragonMidpoint xy_to_n(Xmid+dx,Ymid+dy)

For arms 1 and 3 the two "leaving" edges are up+down on odd points (X+Y odd) or left+right on even points (X+Y even). But for arms 2 and 4 it's the other way around. So without an easy way to identify the arm for an X,Y this probably doesn't help identify which two of the four edges are the desired ones.

Turns

At each point N the curve always turns either left or right, it never goes straight ahead. The bit above the lowest 1 in N gives the turn direction.

    N = 0b...z10000   (possibly no trailing 0s)

    z bit    Turn
    -----    ----
      0      left
      1      right

For example N=12 is binary 0b1100, the lowest 1 bit is 0b_1__ and the bit above that is a 1, which means turn to the right. Or N=18 is binary 0b10010, the lowest 1 is 0b___1_ and the bit above that is 0, so turn left there.

This z bit can be picked out with some bit twiddling

    $mask = $n & -$n;          # lowest 1 bit, 000100..00
    $z = $n & ($mask << 1);    # the bit above it
    $turn = ($z == 0 ? 'left' : 'right');

The bits also give the turn after next by looking at the bit above the lowest 0.

    N = 0b...w01111    (possibly no trailing 1s)

    w bit    Next Turn
    ----     ---------
      0       left
      1       right

For example at N=12=0b1100 the lowest 0 is the least significant bit 0b___0, and above that is a 0 too, so after going to N=13 the turn there at 13 is to the left. Or for N=18=0b10010 the lowest 0 is again the least significant bit, but above it is a 1, so at N=19 the turn is to the right.

This too can be found with some bit twiddling, as for example

    $mask = $n ^ ($n+1);      # low one and below 000111..11
    $w = $n & ($mask + 1);    # the bit above there
    $turn = ($w == 0 ? 'left' : 'right');

There's nothing in the current code for these turn calculations.

Total Turn

The total turn can be calculated from the segment replacements resulting from the bits of N going from high to low.

    plain state
     0 -> no change
     1 -> turn left, go to reversed state

    reversed state
     1 -> no change
     0 -> turn left, go to plain state

This arises from the different side a segment expands on according to plain or reversed state. A segment A to B expands to an "L" bend on the right in plain state, or on the left in reversed state.

      plain state             reverse state

      A = = = = B                    +       
       \       /              0bit  / \      
        \     /               turn /   \ 1bit
    0bit \   / 1bit           left/     \    
          \ /  turn              /       \   
           +   left             A = = = = B

In both cases there's a rotate of +45 degrees at each step which keeps the very first segment of the whole curve in a fixed direction (along the X axis) and this means the south-east slope which is the 0 of plain or the 1 of reversed is no-change, and the north-east slope which is the other new edge is a turn towards the left.

The effect for the bits of N is to count a left turn at each transition from 0 to 1 or back again from 1 to 0. Initial "plain" state means the infinite zero bits at the high end of N are included. For example N=9 is 0b1001 so three left turns for curve direction south to go to N=10 (as can be seen in the diagram above).

     1 00 1   N=9
    ^ ^  ^   
    +-+--+---three transitions,
             so three left turns for direction south

Or the transitions can be viewed as a count of how many blocks of 0s or 1s,

    1 00 1   three blocks of 0s and 1s

This can be calculated by some bit twiddling using a shift and xor to turn a count of transitions into a of 1 bits, as noted by Jorg Arndt (fxtbook section 1.31.3.1).

    total turn = count_1_bits ($n ^ ($n >> 1))

The reversing structure of the curve shows up in the total turn sequence. Each block of 2^N is followed by its own reversal plus 1. For example,

    N=0 to N=7    0, 1, 2, 1, 2, 3, 2, 1

    N=15 to N=8   1, 2, 3, 2, 3, 4, 3, 2    each is +1

OEIS

The Dragon curve is in Sloane's Online Encyclopedia of Integer Sequences in various forms (and see DragonMidpoint too),

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

    A014577 -- turn, 0=left,1=right
    A014707 -- turn, 1=left,0=right
    A014709 -- turn, 2=left,1=right
    A014710 -- turn, 1=left,2=right
    A038189 -- bit above lowest 1, is 0=left,1=right (extra initial 0)
    A082410 -- reversing complement, is 1=left,0=right (extra initial 0)
    A034947 -- Jacobi (-1/n), is turn 1=left,-1=right
    A112347 -- Kronecker (-1/n), is 1=left,-1=right (extra initial 0)
    A121238 -- -1^(n+ some partitions), is 1=left,-1=right (extra 1)

The above turn sequences differ only in having left or right represented as 0, 1 or -1, and possible extra initial 0 or 1 arising from their definitions.

    A005811 -- total turn
    A088748 -- total turn + 1
    A164910 -- cumulative total turn (of A088748)
    A166242 -- double/halving so 2^(total turn)

    A088431 -- turn sequence run lengths
    A007400 --   2*runlength
    A091072 -- odd part 4K+1, is N positions of the left turns
    A126937 -- points numbered like SquareSpiral (with N-1 and flip Y)

The turn sequence run lengths A088431 and A007400 are in fact from a continued fraction expansion of

        1   1   1     1      1              1
    1 + - + - + -- + --- + ----- + ... + ------- + ...
        2   4   16   256   65536         2^(2^k)

The A126937 SquareSpiral numbering has the dragon curve and square spiralling with their Y axes in opposite directions, as can be seen in a126937.pdf of that sequence. So the dragon turns up towards positive Y but the square spiral turns down towards negative Y (or vice versa). PlanePath code for this, starting at $i=0, would be

      my $dragon = Math::PlanePath::DragonCurve->new;
      my $square = Math::PlanePath::SquareSpiral->new;
      my ($x, $y) = $dragon->n_to_xy ($i);
      my $A126937_of_i = $square->xy_to_n ($x, -$y) - 1;

For reference, "dragon-like" A059125 is similar to the turn sequence A014707, but differs in having the "middle" value for each replication come from successive values of the sequence itself (or something like that).

SEE ALSO

Math::PlanePath, Math::PlanePath::DragonRounded, Math::PlanePath::DragonMidpoint, Math::PlanePath::TerdragonCurve, Math::PlanePath::ComplexMinus, Math::PlanePath::ComplexPlus

HOME PAGE

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

LICENSE

Copyright 2011, 2012 Kevin Ryde

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