Math::PlanePath::DragonMidpoint -- dragon curve midpoints
use Math::PlanePath::DragonMidpoint; my $path = Math::PlanePath::DragonMidpoint->new; my ($x, $y) = $path->n_to_xy (123);
This is an integer version of the dragon or paper folding curve by Heighway, Harter, et al, following the midpoint of each edge of the curve segments.
17--16 9---8 5 | | | | 18 15 10 7 4 | | | | 19 14--13--12--11 6---5---4 3 | | 20--21--22 3 2 | | 33--32 25--24--23 2 1 | | | | 34 31 26 0---1 <- Y=0 | | | 35 30--29--28--27 -1 | 36--37--38 43--44--45--46 -2 | | | 39 42 49--48--47 -3 | | | 40--41 50 -4 | 51 -5 | 52--53--54 -6 | ..--64 57--56--55 -7 | | 63 58 -8 | | 62--61--60--59 -9 ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ -10 -9 -8 -7 -6 -5 -4 -3 -2 -1 X=0 1
The dragon curve itself begins as follows, with the edge midpoints at each "*",
--*-- --*-- | | | | * * * * | | | | --*--+--*-- --*-- | | * * | | --*--+ --*-- | ...
The midpoints are on fractions X=0.5,Y=0, X=1,Y=0.5, etc. Those positions can in fact be had from the DragonCurve module by asking for N=0.5, 1.5, 2.5, etc. But for this DragonMidpoint curve they're turned clockwise 45 degrees and shrunk by sqrt(2) to be integer X,Y values stepping by 1.
Because the dragon curve only traverses each edge once the midpoints are all distinct X,Y positions.
The midpoints fill 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.
arms
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.
arms => 4
...-107-103 83--79--75--71 | | | 68--64 36--32 99 87 59--63--67 | | | | | | | 72 60 40 28 95--91 55 | | | | | 76 56--52--48--44 24--20--16 51 | | | 80--84--88 17--13---9---5 12 47--43--39 ... | | | | | | 100--96--92 21 6---2 1 8 27--31--35 106 | | | | | | 104 33--29--25 10 3 0---4 23 94--98-102 | | | | | | ... 37--41--45 14 7--11--15--19 90--86--82 | | | 49 18--22--26 46--50--54--58 78 | | | | | 53 89--93 30 42 62 74 | | | | | | | 65--61--57 85 97 34--38 66--70 | | | 69--73--77--81 101-105-...
With four arms like this every X,Y point is visited exactly once, corresponding to the way four copies of the dragon curve traversing each edge exactly once.
See "FUNCTIONS" in Math::PlanePath for the behaviour common to all path classes.
$path = Math::PlanePath::DragonMidpoint->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.
$n
$n < 0
Fractional positions give an X,Y position along a straight line between the integer positions.
$n = $path->n_start()
Return 0, the first N in the path.
Math::PlanePath, Math::PlanePath::DragonCurve
http://user42.tuxfamily.org/math-planepath/index.html
Copyright 2011 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/>.
To install Math::PlanePath, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Math::PlanePath
CPAN shell
perl -MCPAN -e shell install Math::PlanePath
For more information on module installation, please visit the detailed CPAN module installation guide.