Math::PlanePath::DragonCurve -- dragon curve
use Math::PlanePath::DragonCurve; my $path = Math::PlanePath::DragonCurve->new; my ($x, $y) = $path->n_to_xy (123);
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---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 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 the first arm, N=1,5,9,13 the second, N=2,6,10,14 the third and N=3,7,11,15 the fourth.
arms => 4 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.
The first step N=1 is to the right along the X axis and the path then slowly spirals anti-clockwise and progressively fatter. The end of each replication is N=2^level which is at level*45 degrees around,
N X,Y angle radial dist ---- ----- ----- ----------- 1 1,0 0 1 2 1,1 45 sqrt(2) 4 0,2 90 sqrt(4)=2 8 -2,2 135 sqrt(8) 16 -4,0 180 sqrt(16)=4 32 -4,-4 225 sqrt(32) ...
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 Nlevel=2^level for N=2 or higher, the curve always goes upward from Nlevel-1 to Nlevel, and then goes to the left for Nlevel+1. For example at N=16 the curve goes up N=15 to N=16, then left for N=16 to N=17. Likewise at N=32, etc. The spiral curls around ever further but the self-similar twist back means the Nlevel endpoint is always at this same up/left orientation. See "Total Turn" below for the net direction in general.
The X,Y extents of the path through to Nlevel=2^level can be expressed as a "length" in the direction of the Xlevel,Ylevel endpoint and a "width" across.
level even, so endpoint is a straight line k = level/2 +--+ <- Lmax | | | E <- Lend = 2^k at Nlevel=2^level | +-----+ | O | <- Lstart=0 | | +--+ <- Lmin ^ ^ Wmin Wmax Lmax = (7*2^k - 4)/6 if k even (7*2^k - 2)/6 if k odd Lmin = - (2^k - 1)/3 if k even - (2^k - 2)/3 if k odd Wmax = (2*2^k - 1) / 3 if k even (2*2^k - 2) / 3 if k odd Wmin = Lmin
For example level=2 is to Nlevel=2^2=4 and k=level/2=1 is odd so it measures as follows,
4 <- Lmax = (7*2^1 - 2)/6 = 2 | 3--2 | 0--1 <- Lmin = -(2^1 - 2)/3 = 0 ^ ^Wmax = (2*2^1 - 1)/3 = 1 | Wmin = Lmin = 0
Or level=4 is to Nlevel=2^4=16 and k=4/2=2 is even. It measures as follows. The lengthways "L" measures are in the direction of the N=16 endpoint and the "W" measures are across.
9----8 5---4 <- Wmax = (2*2^2 - 2)/3 = 2 | | | | 10--11,7---6 3---2 | | 16 13---12 0---1 | | 15---14 <- Wmin = -(2^2 - 1)/3 = -1 ^ ^Lmin = Wmin = -1 | Lmax = (7*2^2 - 4)/6 = 4
The formulas are all integer values, but the fractions 7/6, 1/3 and 2/3 show the limits as the level increases. If scaled so that length Lend=2^k is reckoned as 1 unit then Lmax extends 1/6 past the end, Lmin and Wmin extend 1/3, and Wmax extends across 2/3.
+--------+ -- | - | 1/6 total length || | | = 1/6+1+1/3 = 3/2 || E | -- || | || | | \ | 1 | \ | | --\ | | \ | | || | O || -- | | || | | || 1/3 | ---- | +--------+ -- 1/3| 2/3 total width = 1/3+2/3 = 1
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 and 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 after that 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.
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.
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
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 may be up to two Ns for a given
$n = $path->n_start()
Return 0, the first N in the path.
The current code uses the
xy_to_n() by rotating -45 degrees and offsetting to the midpoints of the four edges around the target X,Y. The
DragonMidpoint algorithm then gives four candidate N values and those which convert back to the desired X,Y in the
n_to_xy() are the results for
Xmid,Ymid = X+Y, Y-X # rotate -45 degrees for dx = 0 or -1 for dy = 0 or 1 N candidate = DragonMidpoint xy_to_n(Xmid+dx,Ymid+dy)
Since there's at most two
DragonCurve Ns at a given X,Y the loop can stop when two Ns are found.
Only the "leaving" edges will convert back to the target N, so only two of the four edges actually need to be considered. Is there a way to identify them? For arm 1 and 3 the leaving edges are up,down on odd points (meaning sum X+Y odd) and right,left for even points (meaning sum X+Y even). But for arm 2 and 4 it's the other way around. Without an easy way to determine the arm this doesn't seem to help.
Whether a given X,Y is visited by the curve can be determined from one or two segments (rather then up to four for X,Y to N).
| S midpoint Xmid = X+Y | Ymid = Y-X *---T--X,Y--S---* | T midpoint Xmid-1 | Ymid+1
Segment S is to the East of X,Y. The arm it falls on can be determined as per "X,Y to N" in Math::PlanePath::DragonMidpoint. Numbering arm(S) = 0,1,2,3 then
X,Y Visited ----------- if arms_count()==4 yes # whole plane if arm(S) < arms_count() yes if arm(S)==2 and arms_count()==1 no if arm(T) < arms_count() yes
This works because when two arms touch they approach and leave by a right angle, they don't cross. So two opposite segments S and T identify the two possible arms coming to the X,Y point.
| | \ ---- ---- \ | |
An arm only touches its immediate neighbour, ie. arm-1 or arm+1 mod 4. This means if arm(S)==2 then arm(T) can only be 1,2,3, not 0. So if
arms_count() is 1 then arm(T) cannot be on the curve and no need to run its segment check.
The only exception to the right-angle touching rule is at the origin X,Y = 0,0. In that case Xmid,Ymid = 0,0 is on the first arm and X,Y is correctly determined to be on the curve. If S was not to the East but some other direction away from X,Y then this wouldn't be so.
At each point the curve always turns either left or right, it never goes straight ahead. The bit above the lowest 1-bit 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');
This sequence is mentioned too in Knuth volume 2 "Seminumerical Algorithms" answer to section 4.5.3 question 41 as the "dragon sequence". It's expressed there recursively as
d(0) = 1 # unused, the first turn being at N=1 d(2N) = d(N) # shift down looking for low 1-bit d(4N+1) = 0 # bit above lowest 1-bit is 0 d(4N+3) = 1 # bit above lowest 1-bit is 1
The bits also give the turn after next by looking at the bit above the lowest 0-bit. This works because 011..11 + 1 = 100..00 so the bit above the lowest 0 becomes the bit above the lowest 1.
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 at N=13 the turn 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');
The total turn is the count of 0<->1 transitions in the runs of bits of N, which is the same as how many bit pairs of N (including overlaps) are different so "01" or "10".
This can be seen from the segment replacements resulting from bits of N,
N bits from high to low, start in "plain" state plain state 0 bit -> no change 1 bit -> count left, and go to reversed state reversed state 0 bit -> count left, and go to plain state 1 bit -> no change
The 0 or 1 counts are from the different side a segment expands on in plain or reversed state. Segment A to B expands to an "L" shape bend which is on the right in plain state, but on the left in reversed state.
plain state reverse state A = = = = B + \ ^ 0bit / \ \ / turn / \ 1bit 0bit \ / 1bit left/ \ \ / turn / v + left A = = = = B
In both cases a rotate of +45 degrees keeps the very first segment of the whole curve in a fixed direction (along the X axis), which means the south-east slope shown is no-change. This is the 0 of plain or the 1 of reversed. And the north-east slope which is the other new edge is a turn towards the left.
It can be seen the "state" above is simply the previous bit, so the effect for the bits of N is to count a left turn at each transition from 0->1 or 1->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 N=10 (as can be seen in the diagram above).
1 00 1 N=9 ^ ^ ^ +-+--+---three transitions, so three left turns for direction south
The transitions can also be viewed as a count of how many runs of contiguous 0s or 1s,
1 00 1 three blocks of 0s and 1s
This can be calculated by some bit twiddling with a shift and xor to turn transitions into 1-bits which can then be counted, as noted by Jorg Arndt (fxtbook section 126.96.36.199 "The Dragon Curve").
total turn = count_1_bits ($n ^ ($n >> 1))
The reversing structure of the curve shows up in the total turn at each point. The total turns for a 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 <-------
n_to_dxdy() is the "total turn" per above, or for fractional N then an offset according to the "next turn" above. If you've got the bit twiddling operators described then the two can be calculated separately.
n_to_dxdy() code tries to support floating point or other number types without bitwise XOR etc by processing bits high to low with a state table which combines the calculations for total turn and next turn. The state encodes
total turn 0 to 3 next turn 0 or 1 previous bit 0 or 1 (the bit above the current bit)
The "next turn" remembers the bit above lowest 0 seen so far (or 0 initially). The "total turn" counts 0->1 or 1->0 transitions. For both the "previous bit" shows when there's a transition, or what bit is above when a 0 is seen. It also works not to have this held in the state but instead pick out a bit and the one above it each time.
At the end of bit processing any "previous bit" in state is no longer needed and can be masked out to lookup the final four dx, dy, next dx, next dy.
The boundary length of the curve N=0 to N=2^k inclusive is given by a recurrence relation
B[k+4] = 2*B[k+3] - B[k+2] + 2*B[k+1] - 2*B[k] for k >= 0 starting B = 2 B = 4 B = 8 B = 16 2, 4, 8, 16, 28, 48, 84, 144, 244, 416, 708, 1200, 2036, ...
The first few boundary lengths double with N=2^k but when the curve begins to touch itself the boundary is less than 2x. This happens at B=28 onwards.
The boundary formula can be obtained by taking the curve in five types of section,
R initial values 8 5 <--- 4 k L R T U V | ^ | --- -- -- -- -- -- R | U | V | T 0 1 1 2 3 3 v | v 1 2 2 4 6 7 <---- 6 3 <--- 2 2 4 4 8 L | 3 8 8 12 U | L 4 16 20 v 0 ---> 1 R
L and R are the left and right sides of the curve so the total is
total boundary B[k] = L[k] + R[k]
The other parts T, U and V are because the points measured from must be on the boundary. The way the curve touches itself within the "U" part means only 0 and 3 are on the left boundary and so a measurement must be made between those only. Similarly T and V.
The arrowheads drawn show the direction of the curve section in its replication. Sections U and V have different directions within their "U" shape and so are not the same. If rotated to upright then U has positions odd,even whereas V is even,odd,
even odd even odd odd even 8 5 0 3 3 6 | U | | U | | V | 7---6 1---2 4---5
The curve expands from 8 sections to 16 sections in the following pattern. (Drawn here turned 45-degrees to stay horizontal and vertical.)
R R * <--- 4 * <--- 2 ^ | ^ | L | | U | V | T | v | v 5 ---> * <---- 3 * <--- 1 ^ | V | T U | L | v 8 * <--- 6 0 ---> * | ^ R R | U | T v | * <--- 7 L
It can be seen R -> R+L, L -> T, T -> U+R, U -> U+V, and V->T. For V (3 to 6) the curve touches itself and so closes off some boundary leaving just T. The effect is that the V values are V=3 and thereafter the T values V=T, V=T, etc.
These expansions can be written as recurrences
R[k+1] = R[k] + L[k] for k >= 0 L[k+1] = T[k] T[k+1] = R[k] + U[k] U[k+1] = U[k] + V[k] V[k+1] = T[k]
L[k+1]=T[k] and V[k+1]=T[k] so L[k]=V[k] for k>=1. Replace V by L to eliminate V. This is only for k>=1 since for k=0 L=1 and V=3 are not equal.
R[k+1] = R[k] + L[k] for k >= 1 L[k+1] = T[k] T[k+1] = R[k] + U[k] U[k+1] = U[k] + L[k]
Then replace T[k] by L[k+1] to eliminate T. (The resulting L[k+1]=R[k-1]+U[k-1] is also two expansions of the left side, as shown further below.)
R[k+1] = R[k] + L[k] for k >= 1 L[k+1] = R[k-1] + U[k-1] U[k+1] = U[k] + L[k]
The second equation gives U[k-1]=L[k+1]-R[k-1]. Substitute that twice into the third equation to eliminate U,
R[k+1] = R[k] + L[k] for k >= 1 L[k+3]-R[k+1] = L[k+2]-R[k] + L[k]
Finally the first equation gives L[k]=R[k+1]-R[k] which eliminates L from the second. The result is a fourth-order recurrence for R
R[k+4] = 2*R[k+3] - R[k+2] + 2*R[k+1] - 2*R[k] k >= 1 starting R = 1 R = 2 R = 4 R = 8 R = 16 1, 2, 4, 8, 16, 28, 48, 84, 144, 244, 416, 708, 1200, 2036, ...
The recurrence is only for k>=1 as noted. At k=0 it would give R=14 but that's incorrect, N=0 to N=16 has right side boundary R=16.
The total boundary B[k]=R[k]+L[k] is the same as R[k+1]=R[k]+L[k], so the total boundary is the same as the right side boundary of the next bigger level. Because it's R[k+1] the recurrence for B given at the start of the section becomes for k>=0.
B[k] = R[k+1] for k>=0 = 2, 4, 8, 16, 28, 48, 84, 144, 244, 416, 708, 1200, ...
L can be obtained by by rearranging the R[k+4] recurrence to pair up R[k+1]-R[k]. (which is the boundary increment from k to k+1.)
R[k+4]-R[k+3] = R[k+3]-R[k+2] + 2*(R[k+1]-R[k])
Then L[k]=R[k+1]-R[k] as from above gives a recurrence for L, this time a third-order.
L[k+3] = L[k+2] + 2*L[k] for k >= 1 starting L = 1 L = 2 L = 4 L = 8 1, 2, 4, 8, 12, 20, 36, 60, 100, 172, 292, 492, 836, 1420, ...
U can be obtained by eliminating L and R in a similar way to what was done for R above. The result is the same recurrence as R but with different initial values.
U[k+4] = 2*U[k+3] - U[k+2] + 2*U[k+1] - 2*U[k] for k >= 1 starting U = 3 U = 6 U = 8 U = 12 U = 20 3, 6, 8, 12, 20, 32, 52, 88, 148, 248, 420, 712, 1204, 2040, ...
Or U can be obtained by considering a left side expansion U in terms of an L,R difference (and this is also in the substitutions made above).
* 4 L[k+2] = U[k] + R[k] | | | R | so R[k+2] | v | 3 <--- 2 U[k] = L[k+2] - R[k] | | | U | v v * 0 ---> 1
A boundary calculation for the curve as a fractal of infinite descent can also be found in Chang and Zhang,
Angel Chang and Tianrong Zhang, "The Fractal Geometry of the Boundary of Dragon Curves", Journal of Recreational Mathematics, volume 30, number 1, 1999-2000, pages 9-22. http://www.coiraweb.com/poignance/math/Fractals/Dragon/Bound.html http://stanford.edu/~angelx/pubs/dragonbound.pdf
The area enclosed by the dragon curve N=0 to N=2^k, written in terms of the boundary B[k], is
A[k] = 2^(k-1) - B[k]/4 = 0, 0, 0, 0, 1, 4, 11, 28, 67, 152, 335, 724, 1539, ...
This can be calculated using the fact that each enclosed unit square has its four sides traversed precisely once each. Imagine each line segment as a diamond shape made from two right triangles
* / \ 2 triangles each line segment 0---1 \ / *
If a line segment is on the curve boundary then the outside triangle does not count towards the area. Subtract 1 for each of them.
triangles = 2*2^k - B[k]
Four triangles make up a unit square, so
area[k] = triangles/4 = 2^(k-1) - B[k]/4
The Dragon curve is in Sloane's Online Encyclopedia of Integer Sequences in various forms (and see
DragonMidpoint for its forms too),
A038189 turn, 0=left,1=right, bit above lowest 1, extra 0 A089013 same as A038189, but initial extra 1 A082410 turn, 1=left,0=right, reversing complement, extra 0 A099545 turn, 1=left,3=right, as [odd part n] mod 4 so turn by 90 degrees * 1 or 3 A034947 turn, 1=left,-1=right, Jacobi (-1/n) A112347 turn, 1=left,-1=right, Kronecker (-1/n), extra 0 A121238 turn, 1=left,-1=right, -1^(n + some partitions) extra 1 A014577 next turn, 0=left,1=right A014707 next turn, 1=left,0=right A014709 next turn, 2=left,1=right A014710 next turn, 1=left,2=right A005811 total turn A088748 total turn + 1 A164910 cumulative [total turn + 1] A166242 2^(total turn), by double/halving A088431 turn sequence run lengths A007400 2*runlength A091072 N positions of the left turns, being odd part form 4K+1 A126937 points numbered like SquareSpiral (start N=0 and flip Y) A003460 turns N=1 to N=2^n-1 packed as bits 1=left,0=right low to high, then written in octal A146559 X at N=2^k, for k>=1, being Re((i+1)^k) A009545 Y at N=2^k, for k>=1, being Im((i+1)^k) A227036 boundary length N=0 to N=2^k also boundary length of right side to N=2^(k+1) A203175 boundary length left side N=0 to N=2^k also differences of total boundary A003230 area enclosed N=0 to N=2^k A003478 area differences (increments)
The numerous turn sequences differ only in having left or right represented as 0, 1, -1, etc, and possibly "extra" initial 0 or 1 at n=0 arising from the definitions and the first turn being at n=N=1. The "next turn" forms begin at n=0 for the turn at N=1 and so are the turn at N=n+1.
The run lengths A088431 and A007400 are from a continued fraction expansion of an infinite sum
1 1 1 1 1 1 1 + - + - + -- + --- + ----- + ... + ------- + ... 2 4 16 256 65536 2^(2^k)
Jeffrey Shallit and independently M. Kmosek show how continued fraction terms which are repeated in reverse give rise to this sort of power sum,
Jeffrey Shallit, "Simple Continued Fractions for Some Irrational Numbers", http://www.cs.uwaterloo.ca/~shallit/Papers/scf.ps
SquareSpiral numbering has the dragon curve and square spiralling with their Y axes in opposite directions, as shown in its a126937.pdf. So the dragon curve turns up towards positive Y but the square spiral is numbered 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 (n_start => 0); my ($x, $y) = $dragon->n_to_xy ($i); my $A126937_of_i = $square->xy_to_n ($x, -$y);
For reference, "dragon-like" A059125 is similar to the turn sequence A014707, but differs in having the "middle" values for each replication come from successive values of the sequence itself, or something like that.
Copyright 2011, 2012, 2013, 2014 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/>.