Math::PlanePath::AR2W2Curve -- 2x2 self-similar curve of four patterns
use Math::PlanePath::AR2W2Curve; my $path = Math::PlanePath::AR2W2Curve->new; my ($x, $y) = $path->n_to_xy (123);
This is an integer version of the AR2W2 curve per
Asano, Ranjan, Roos, Welzl and Widmayer "Space-Filling Curves and Their Use in the Design of Geometric Data Structures", Theoretical Computer Science, volume 181, issue 1, pages 3-15, July 1997.
And in LATIN'95 Theoretical Informatics which is at Google Books http://books.google.com.au/books?id=_aKhJUJunYwC&pg=PA36
It traverses the first quadrant in self-similar 2x2 blocks which are a mixture of "U" and "Z" shapes. The mixture is designed to improve some locality measures (how big the N range for a given region).
| 7 42--43--44 47--48--49 62--63 \ | | | | 6 40--41 45--46 51--50 61--60 | | | 5 39 36--35--34 52 55--56 59 | | / | | | | 4 38--37 33--32 53--54 57--58 \ 3 6-- 7-- 8 10 31 28--27--26 | |/ | | | | 2 5-- 4 9 11 30--29 24--25 | | | 1 2-- 3 13--12 17--18 23--22 \ | | | | Y=0 -> 0-- 1 14--15--16 19--20--21 X=0 1 2 3 4 5 6 7
There's four base patterns A to D. A2 is a mirror image of A1, B2 a mirror of B1, etc. The start is A1, and above that D2, then A1 again, alternately.
^----> ^ 2---3 C1 | B2 1 3 C2 D1 | A1 \ | A2 | \ | ----> | 0---1 ^ 0 2 ^ ----> D2 | B1 |B1 B2 ---->| | 1---2 C2 B1 1---2 B2 C1 B1 | | ---->----> B2 | | ---->----> 0 3 ^ | 0 3 ^ | |D1 B2| |B1 D2| | v | v ^ \ ^ | 1---2 B1| \A1 1---2 A2/ | B2 C1 | | | v C2 | | / v 0 3 ^ | 0 3 ^ \ /A2 B2| |B1 \A1 / v | v ^ | ^ \ 1---2 A2/ | C2 1---2 C1| \A1 D1 | | / v D2 | | | v 0 3 ^ \ 0 3 ^ | |D1 \A2 /A1 D2| | v / v
For parts which fill on the right such as the B1 and B2 sub-parts of A1, the numbering must be reversed. This doesn't affect the shape of the curve as such, but it matters for enumerating it as done here.
The default starting shape is the A1 "Z" part, and above it D2. Notice the starting sub-part of D2 is A1 and in turn the starting sub-part of A1 is D2, so those two alternate at successive higher levels. Their sub-parts reach all other parts (in all directions, and forward or reverse).
The start_shape => $str
option can select a different starting shape. The choices are
"A1" \ pair "D2" / "B2" \ pair "B1rev" / "D1rev" \ pair "A2rev" /
B2 begins with a reversed B1 and in turn a B1 reverse begins with B2 (no reverse), so those two alternate. Similarly D1 reverse starts with A2 reverse, and A2 reverse starts with D1 reverse.
The curve is conceived by the authors as descending into ever-smaller sub-parts and for that any of the patterns can be a top-level start. But to expand outwards as done here the starting part must be the start of the pattern above it, and that's so only for the 6 listed. The descent graph is
D2rev -----> D2 <--> A1 B2rev -----> C2rev --> A1rev -----> B2 <--> B1rev <----- C2 C1rev -----> <----- A2 <-- C1 B1 -----> D1rev <--> A2rev D1 ----->
So for example B1 is not at the start of anything. Or A1rev is at the start of C2rev, but then nothing starts with C2rev. Of the 16 total only the three pairs shown "<-->" are cycles and can thus extend upwards indefinitely.
See "FUNCTIONS" in Math::PlanePath for behaviour common to all path classes.
$path = Math::PlanePath::AR2W2Curve->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_lo, $n_hi) = $path->rect_to_n_range ($x1,$y1, $x2,$y2)
The returned range is exact, meaning $n_lo
and $n_hi
are the smallest and largest in the rectangle.
($n_lo, $n_hi) = $path->level_to_n_range($level)
Return (0, 4**$level - 1)
.
Math::PlanePath, Math::PlanePath::HilbertCurve, Math::PlanePath::PeanoCurve
http://user42.tuxfamily.org/math-planepath/index.html
Copyright 2011, 2012, 2013, 2014, 2015, 2016, 2017 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/>.