Math::...nCurve(3pm)
π Math::PlanePath::DragonCurve (3pm) β dragon curve π
okay bestie, welcome to the Math::PlanePath::DragonCurve man page, straight out of User Contributed Perl Documentation. It's the dragon curve and she's literally iconic. Let's get into it. π
πΈ NAME πΈ
Math::PlanePath::DragonCurve β dragon curve. That's it. That's the name. Periodt.
β¨ SYNOPSIS β¨
use Math::PlanePath::DragonCurve;
my $path = Math::PlanePath::DragonCurve->new;
my ($x, $y) = $path->n_to_xy (123);
Three lines and you're already on the dragon curve. No cap.
π DESCRIPTION π
okay so this is the dragon curve, also known as the paper folding curve, by Heighway, Harter, et al. It's giving legendary fractal energy fr fr.
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. So she's messy but not chaotic. π
π¦ 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 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. Obsessed.
π Level Angle π
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.
π Level Ranges π
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 - 2) / 3 if k even
(2*2^k - 1) / 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
π 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 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.
π₯ FUNCTIONS π₯
See "FUNCTIONS" in Math::PlanePath for behaviour common to all path classes.
$path = Math::PlanePath::DragonCurve->new ()$path = Math::PlanePath::DragonCurve->new (arms => $int)
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$non the path. Points begin at 0 and if$n < 0then the return is an empty list. Fractional$ngives 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,$ythen returnundef. The curve visits an$x,$ytwice for various points (all the "inside" points). The smaller of the two N values is returned.@n_list = $path->xy_to_n_list ($x,$y)β Return a list of N point numbers for coordinates$x,$y. The origin 0,0 hasarms_count()many N since it's the starting point for each arm. Other points have up to two Ns for a given$x,$y. If arms=4 then every$x,$yexcept the origin has exactly two Ns.$n = $path->n_start()β Return 0, the first N in the path.
π Level Methods π
($n_lo, $n_hi) = $path->level_to_n_range($level)β Return(0, 2**$level), or for multiple arms return(0, $arms * 2**$level + ($arms-1)).
There are 2^level segments in a curve level, so 2^level+1 points numbered from 0. For multiple arms there are arms*(2^level+1) points, numbered from 0 so n_hi = arms*(2^level+1)-1.
π FORMULAS π the math era
okay bestie so there are SO many formulas for coordinates, lengths and area, and if you're the kind of person who wants the full β¨academicβ¨ treatment, the author wrote a whole long mathematical write-up about it right here:
<http://user42.tuxfamily.org/dragon/index.html>
go off, queen of citations π
πΈ X,Y to N πΈ
the current code is lowkey doing a little trick here β it uses the DragonMidpoint xy_to_n() by rotating -45 degrees and offsetting to the midpoints of the four edges around the target X,Y. then DragonMidpoint hands you four candidate N values, and whichever ones convert back to the X,Y you actually wanted in DragonCurve's n_to_xy() β those are your results for xy_to_n_list(). periodt.
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 any given X,Y, the loop can just stop the second it finds two Ns. no need to be extra about it π
only the "leaving" edges convert back to the target N, so like, only two of the four edges actually need to be considered. is there a way to identify them? bestie, 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. and without an easy way to determine the arm... this doesn't really help. π
π¦ X,Y is Visited π¦
whether a given X,Y is visited by the curve can be figured out from one or two segments (rather than up to four for X,Y to N). iconic efficiency honestly.
| 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, without crossing. so two opposite segments S and T identify the two possible arms coming to the X,Y point. slay, geometry.
|
|
\
---- ----
\
|
|
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 there's 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. and if S was not to the East but some other direction away from X,Y then this wouldn't be so. noted, bestie π
β¨ Turn β¨
at each point the curve always turns either left or right β it never goes straight ahead. and the bit above the lowest 1-bit in N gives you the turn direction. yes, one single bit is doing all that. main character energy fr fr.
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. see? it's giving bit wizardry π§
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 shows up in Knuth volume 2 "Seminumerical Algorithms" answer to section 4.5.3 question 41 and is called the "dragon sequence". it's expressed there recursively as:
d(0) = 1 # unused, since first turn 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
π Next Turn π
the bits also give you 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. math is literally so clever, i can't π
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');
π Total Turn π
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". obsessed with this definition actually.
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 come 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. πΈ
you can see 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. the 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, up to the highest 1-bit.
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 per Jorg Arndt (fxtbook section 1.31.3.1 "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 dX,dY π
n_to_dxdy() is the "total turn" per above, or for fractional N then an offset according to the "next turn" above. if you're using the bit twiddling operators described, then the two can be calculated separately. β¨
the current 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. 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 previous bit in the state but instead pick out two bits 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. and that's the whole vibe check π
β¨ OEIS β¨
okay so the Dragon curve is literally all over Sloane's Online Encyclopedia of Integer Sequences β like, in so many forms it's giving main character energy. (and yes, peep "DragonMidpoint" for its forms too, bestie π )
<http://oeis.org/A014577> (etc)
A332383β X coordinateA332384β Y coordinateA038189β turn, 0=left,1=right, bit above lowest 1, extra 0A089013β turn, 0=left,1=right, bit above lowest 1, extra 1A082410β turn, 1=left,0=right, reversing complement, extra 0A099545β turn, 1=left,3=right, as [odd part n] mod 4 so turn by 90 degrees * 1 or 3A034947β turn, 1=left,-1=right, Jacobi (-1/n)A112347β turn, 1=left,-1=right, Kronecker (-1/n), extra 0A121238β turn, 1=left,-1=right, -1^(n + some partitions) extra 1A119972β turn, n=left,-n=rightA014577β next turn, 1=left,0=rightA014707β next turn, 0=left,1=rightA014709β next turn, 1=left,2=rightA014710β next turn, 2=left,1=rightA090678β 1=same turn as previous, 0=differentA143347β paperfolding constant, bits 0=left,1=right in decimal
These 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 turn at N=1 and so are the turn at N=n+1.
A005811β direction total turnA088748β direction total turn + 1A037834β direction total turn - 1A136004β direction total turn + 4A246960β direction 0,1,2,3 = total turn mod 4A173318β cumulative(total turn)A164910β cumulative(total turn + 1)A166242β 2^(total turn), by double/halvingA000975β N of new maximum total turn, binary 10101...A268411β direction of horizontals, 0=East, 1=WestA043724β N of EastA043725β N of NorthA043726β N of WestA043727β N of SouthA088431β turn sequence run lengthsA007400β 2*runlengthA091072β N positions of the left turns, being odd part form 4K+1A091067β N positions of the right turns, being odd part form 4K+3A255068β N positions where next turn rightA060833β N positions where previous turn rightA106837β N positions of consecutive turns R,RA106838β N positions of consecutive turns R,R,RA106840β N positions of consecutive turns L,LA106841β N positions of consecutive turns L,L,LA106836β N steps between right turnsA088742β N steps between left turnsA255070β num right turns 1 to NA236840β 2* num right turns 1 to NA003460β turns N=1 to N=2^n-1 packed as bits 1=left,0=right low to high, then written in octalA126937β coordinates coded by SquareSpiral (start N=0 and flip Y)A038503β num segments East in level kA038504β num segments North in level kA038505β num segments West in level kA000749β num segments South in level kA146559β 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 right boundary length to N=2^(k+1)A203175β left boundary length N=0 to N=2^k = differences of total boundary = squares on left boundaryA003476β squares on right boundary = single points N=0 to N=2^(k-1) inclusiveA164395β single points N=0 to N=2^k-1 inclusive, for k=4 upA003230β area enclosed N=0 to N=2^k, for k=4 up same as double pointsA003478β area enclosed by left side, also area incrementA003477β area of each blob (touching enclosed unit squares)A003479β join area between N=2^k replicationsA003229β join area increment, also area left side extra over doublingA077949β sameA289265β growth rate r = 1.695 of boundaries etcA272031β fractal dimension log(r)/log(sqrt(2))arms=4A165211β abs(dY), 0,1,0,1,1,0,1,0 repeating
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 some such.
π A088431 and A007400 π
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 repeated in reverse give rise to this sort of power sum,
Jeffrey Shallit, "Simple Continued Fractions for Some Irrational Numbers", Journal of Number Theory, volume 11, 1979, pages 209-217.<http://www.cs.uwaterloo.ca/~shallit/papers.html>,<http://www.cs.uwaterloo.ca/~shallit/Papers/scf.ps>
(And which appears in Knuth "Art of Computer Programming", volume 2, section 4.5.3 exercise 41.)
πΈ A126937 πΈ
The A126937 "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);
π HOUSE OF GRAPHS π
House of Graphs entries for the dragon curve as a graph include
<https://hog.grinvin.org/ViewGraphInfo.action?id=19655> etc
19655β level=0 (1-segment path)32234β level=1 (2-segment path)286β level=2 (4-segment path)414β level=3 (8-segment path)33739β level=433741β level=533743β level=633745β level=733747β level=8
And for just a blob (the biggest 2-connected component in its level)
674β level=4 (4-cycle single unit square)25223β level=533749β level=633751β level=733753β level=834163β level=9
β¨ SEE ALSO β¨
Math::PlanePathMath::PlanePath::DragonRoundedMath::PlanePath::DragonMidpointMath::PlanePath::R5DragonCurveMath::PlanePath::TerdragonCurveMath::PlanePath::ComplexMinusMath::PlanePath::ComplexPlusMath::PlanePath::CCurveMath::PlanePath::AlternatePaperGraph::Maker::TwindragonAreaTree
<http://rosettacode.org/wiki/Dragon_curve>
π HOME PAGE π
<http://user42.tuxfamily.org/math-planepath/index.html>
π LICENSE π
Copyright 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020, 2021 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/>.
π That's the tea, bestie! This page is from perl v5.32.0, dated 2021-01-23, and it's the Math::...nCurve(3pm) manpage. Stay iconic! π
β¨
π¦ where this page comes from
- debian bookworm: libmath-planepath-perl 129-1 Β· GPL-3.0-or-later
- debian trixie: libmath-planepath-perl 129-1 Β· GPL-3.0-or-later
- ubuntu jammy: libmath-planepath-perl 129-1 Β· GPL-3.0-or-later
- ubuntu noble: libmath-planepath-perl 129-1 Β· GPL-3.0-or-later
- ubuntu resolute: libmath-planepath-perl 129-1 Β· GPL-3.0-or-later
- ubuntu stonking: libmath-planepath-perl 129-1 Β· GPL-3.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms