Math::PlanePath::HilbertCurve -- self-similar quadrant traversal
use Math::PlanePath::HilbertCurve; my $path = Math::PlanePath::HilbertCurve->new; my ($x, $y) = $path->n_to_xy (123);
This path by David Hilbert traverses a quadrant of the plane one step at a time in a self-similar pattern,
... | y=7 63--62 49--48--47 44--43--42 | | | | | y=6 60--61 50--51 46--45 40--41 | | | y=5 59 56--55 52 33--34 39--38 | | | | | | | y=4 58--57 54--53 32 35--36--37 | y=3 5---6 9--10 31 28--27--26 | | | | | | | y=2 4 7---8 11 30--29 24--25 | | | y=1 3---2 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
The start is a sideways U shape per 0,1,2,3, and then four of those are put together in an upside-down U. The orientation of the sub parts are chosen so the starts and ends are adjacent, so 3 next to 4, 7 next to 8, and 11 next to 12.
5,6___9,10 4,7 8,11 | | 3,2 13,12__ 0,1 14,15
The process repeats, doubling in size each time and alternately sideways or upside-down U at the top level and invert or transposes as necessary in the sub-parts.
The pattern is sometimes drawn with the first step 0->1 upwards instead of to the right. Right is used here since that's what most of the other PlanePaths do. Swap X and Y for upwards first instead.
Within a power-of-2 square 2x2, 4x4, 8x8, 16x16 etc 2^k, all the N values 0 to 2^(2*k)-1 are within the square. The maximum 3, 15, 63, 255 etc 2^(2*k)-1 is alternately at the top left or bottom right corner.
Because each N step is by 1, the distance along the curve between two X,Y points is the difference in their N values (per xy_to_n).
xy_to_n
The Hilbert curve is fairly localized, so a small rectangle (or other shape) is usually a small range of N. This property is used in some database systems to store X,Y coordinates with an index generated from the Hilbert N. A search through an X,Y region is then usually a fairly modest linear N search.
The N range can be large when crossing Hilbert sub-parts though. In the sample above it can be seen for instance adjacent points x=0,y=3 and x=0,y=4 have rather widely spaced N values 5 and 58.
Fractional X,Y values can be indexed by extending the N calculation down into the X,Y binary fractions. The code here is not directly setup to do this, except if X,Y fractions are turned into integers by moving the binary point an even number of places, the same amount in each, and the resulting integer N moved back by a corresponding multiple of 4 places.
The Hilbert Curve path is in Sloane's OEIS in several forms, eg.
http://oeis.org/A163355 A163365 diagonals summed A163477 diagonals summed, divided by 4 A163482 row at Y=0 A163483 column at X=0 A163538 X change -1,0,1 A163539 Y change -1,0,1 A163540 absolute direction of each step (up,down,left,right) A163541 absolute direction, transpose X,Y A163542 relative direction (ahead,left,right) A163543 relative direction, transpose X,Y
And taking squares of the plane in various orders, each value the N of the Peano curve at those positions.
A163355 in the ZOrderCurve sequence A163357 in diagonals like Math::PlanePath::Diagonals with first Hilbert step along same axis the diagonals start A163359 in diagonals, transposed to start along the opposite axis A163361 A163357 + 1, numbering the Hilbert N's from N=1
The sequences are in each case permutations of the integers since all X,Y positions are reached eventually. The inverses are
A163356 inverse of A163355 A163358 inverse of A163357 A163360 inverse of A163359 A163362 inverse of A163361
Converting N to X,Y coordinates is reasonably straightforward. The top two bits of N is a configuration
3--2 1--2 | or transpose | | 0--1 0 3
according to whether it's an odd or even bit-pair position. Within the "3" sub-parts there's also inverted forms
1--0 3 0 | | | 2--3 2--1
Working N from high to low with a state variable can record whether there's a transpose, an invert, or both (four states altogether). A bit pair 0,1,2,3 from N then gives a bit each of X,Y according to the configuration, and a new state which is the orientation of the sub-part. Gosper's HAKMEM item 115 has this with tables for the state and X,Y bits,
http://www.inwap.com/pdp10/hbaker/hakmem/topology.html#item115
And C++ code based on that in Jorg Arndt's book,
http://www.jjj.de/fxt/#fxtbook (section 1.31.1)
It also works to process N from low to high, at each stage applying any transpose (swap X,Y) and/or invert (bitwise negate) to the low X,Y bits generated so far. This approach saves locating the top bits of N, but if using bignums then the bitwise inverts will be much more work.
The reverse X,Y to N can follow the table approach from high to low taking one bit from X and Y each time. The state table of N-pair -> X-bit,Y-bit is reversible, and a new state is based on the N-pair thus obtained (or could be based on the X,Y bits if that mapping was combined in the state transition table).
The current code is a mixture of the low to high for n_to_xy but the table high to low for the reverse xy_to_n.
n_to_xy
The range of N occurring in a rectangle (rect_to_n_range) can be found in a similar way to converting X,Y coordinates to N. Start at a bit position bigger than the rectangle and look at which of the 4 Hilbert curve sub-parts overlap the rectangle, and choose the one with the biggest (or smallest) N bits. The biggest and smallest must be done separately as they track down different N bits and thus different state transitions. The N range for any shape can be done this way, it only depends on asking which sub-parts overlap or not with the target area.
rect_to_n_range
Each step between successive N values is by 1 up, down, left or right. The next direction can be calculated from the N position with on some base-4 digit-3s parity of N and -N (twos complement). C++ code in Jorg Arndt's fxtbook per above.
$path = Math::PlanePath::HilbertCurve->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. Integer positions are always just 1 apart either horizontally or vertically, so the effect is that the fraction part appears either added to or subtracted from X or Y.
$n = $path->xy_to_n ($x,$y)
Return an integer point number for coordinates $x,$y. Each integer N is considered the centre of a unit square an $x,$y within that square returns N.
$x,$y
($n_lo, $n_hi) = $path->rect_to_n_range ($x1,$y1, $x2,$y2)
Return a range of N values which occur in a rectangle with corners at $x1,$y1 and $x2,$y2. If the X,Y values are not integers then the curve is treated as unit squares centred on each integer point and squares partly covered by the given rectangle are included.
$x1
$y1
$x2
$y2
The returned range is exact, meaning $n_lo is the smallest in the rectangle and $n_hi is the biggest. Of course not all the N's in the range are necessarily in the rectangle.
$n_lo
$n_hi
Math::PlanePath, Math::PlanePath::ZOrderCurve
Math::Curve::Hilbert
David Hilbert, "Ueber die stetige Abbildung einer Line auf ein Flächenstück", Mathematische Annalen, volume 38, number 3, p459-460,
DOI 10.1007/BF01199431 http://www.springerlink.com/content/v1u6427kk33k8j56/ http://notendur.hi.is/oddur/hilbert/gcs-wrapper-1.pdf
http://user42.tuxfamily.org/math-planepath/index.html
Math-PlanePath is Copyright 2010, 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/>.
1 POD Error
The following errors were encountered while parsing the POD:
Non-ASCII character seen before =encoding in 'Flächenstück'. Assuming CP1252
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.