- SEE ALSO
- HOME PAGE
Math::PlanePath::HilbertCurve -- 2x2 self-similar quadrant traversal
use Math::PlanePath::HilbertCurve; my $path = Math::PlanePath::HilbertCurve->new; my ($x, $y) = $path->n_to_xy (123);
This path is an integer version of the curve described by David Hilbert in 1891 for filling a unit square. It traverses a quadrant of the plane one step at a time in a self-similar 2x2 pattern,
... | | 7 | 63--62 49--48--47 44--43--42 | | | | | | 6 | 60--61 50--51 46--45 40--41 | | | | 5 | 59 56--55 52 33--34 39--38 | | | | | | | | 4 | 58--57 54--53 32 35--36--37 | | 3 | 5---6 9--10 31 28--27--26 | | | | | | | | 2 | 4 7---8 11 30--29 24--25 | | | | 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 N=0 to N=3, then four of those are put together in an upside-down U as
5,6 9,10 4,7--- 8,11 | | 3,2 13,12 0,1 14,15--
The orientation of the sub parts ensure the starts and ends are adjacent, so 3 next to 4, 7 next to 8, and 11 next to 12.
The process repeats, doubling in size each time and alternately sideways or upside-down U with invert and/or transpose 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.
See examples/hilbert-path.pl in the Math-PlanePath sources for a sample program printing the path pattern in ascii.
Within a power-of-2 square 2x2, 4x4, 8x8, 16x16 etc (2^k)x(2^k) at the origin, 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 step is by 1, the distance along the curve between two X,Y points is the difference in their N values (as from
On the X=Y diagonal N=0,2,8,10,32,etc is the integers using only digits 0 and 2 in base 4, or equivalently have even-numbered bits 0, like x0y0...z0.
The Hilbert curve is fairly well localized in the sense that 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 the Hilbert curve N as an index. A search through an 2-D region is then usually a fairly modest linear search through N values.
rect_to_n_range() gives exact N range for a rectangle, or see "Rectangle to N Range" below for calculating on any shape.
The N range can be large when crossing sub-parts. 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 X,Y binary fractions. The code here doesn't do that, but could be pressed into service by moving the binary point in X and Y an even number of places, the same in each. (An odd number of bits would require swapping X,Y to compensate for the alternating transpose in part 0.) The resulting integer N is then divided down by a corresponding multiple-of-4 binary places.
The Hamming distance between integers X and Y is the number of bit positions where the two values differ when written in binary. On the Hilbert curve each bit-pair of N becomes a bit of X and a bit of Y,
N X Y ------ --- --- 0 = 00 0 0 1 = 01 1 0 <- difference 1 bit 2 = 10 1 1 3 = 11 0 1 <- difference 1 bit
So the Hamming distance for N=0to3 is 1 at N=1 and N=3. As higher levels these the X,Y bits may be transposed (swapped) or rotated by 180 or both. A transpose swapping X<->Y doesn't change the bit difference. A rotate by 180 is a flip 0<->1 of the bit in each X and Y, so that doesn't change the bit difference either.
On that basis the Hamming distance X,Y is the number of base4 digits of N which are 01 or 11. If bit positions are counted from 0 for the least significant bit then
HammingDist(X,Y) = count 1-bits at even bit positions in N
See also "Hamming Distance" in Math::PlanePath::CornerReplicate which has the same formula, but arising directly from 01 or 11, no transpose or rotate.
See "FUNCTIONS" in Math::PlanePath for behaviour common to all path classes.
$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
$non the path. Points begin at 0 and if
$n < 0then the return is an empty list.
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 is an offset along either
$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 and an
$x,$ywithin that square returns N.
($n_lo, $n_hi) = $path->rect_to_n_range ($x1,$y1, $x2,$y2)
The returned range is exact, meaning
$n_hiare the smallest and biggest in the rectangle.
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. Then within each of 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, being 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 that sub-part. William Gosper's HAKMEM item 115 has this with tables for the state and X,Y bits,
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 NOT) to the low X,Y bits generated so far. This works because there's no "reverse" sections, or since the curve is the same forward and reverse. Low to high saves locating the top bits of N, but if using bignums then the bitwise inverts of the full X,Y values will be much more work.
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 is combined into the state transition table).
An easy over-estimate of the maximum N in a region can be had by finding the next bigger (2^k)x(2^k) square enclosing the region. This means the biggest X or Y rounded up to the next power of 2, so
find lowest k with 2^k > max(X,Y) N_max = 2^(2k) - 1
Or equivalently rounding down to the next lower power of 2,
find highest k with 2^k <= max(X,Y) N_max = 2^(2*(k+1)) - 1
An exact N range can be found by following the high to low N to X,Y procedure above. Start at the 2^(2k) bit pair position in an N bigger than the desired region and choose 2 bits for N to give a bit each of X and Y. The X,Y bits are based on the state table as above and the bits chosen for N are those for which the resulting X,Y sub-square overlaps some of the target region. The smallest N similarly, choosing the smallest bit pair for N which overlaps.
The biggest and smallest N digit for a sub-part can be found with a lookup table. The X range might cover one or both sub-parts, and the Y range similarly, for a total 9 possible configurations. Then a table of state+coverage -> digit gives the minimum and maximum N bit-pair, and state+digit gives a new state the same as X,Y to N.
Biggest and smallest N must be calculated with separate state and X,Y values since they track down different N bits and thus different states. But they take the same number of steps from an enclosing level down to level 0 and can thus be done in a single loop.
The N range for any shape can be found this way, not just a rectangle like
rect_to_n_range(). At each level the procedure only depends on asking which combination of the four sub-parts overlaps some of the target area.
Each step between successive N values is always 1 up, down, left or right. The next direction can be calculated from N in the high-to-low procedure above by watching for the lowest non-3 digit and noting the direction from that digit towards digit+1. That can be had from the state+digit -> X,Y table looking up digit and digit+1, or alternatively a further table encoding state+digit -> direction.
The reason for taking only the lowest non-3 digit is that in a 3 sub-part the direction it goes is determined by the next higher level. For example at N=11 the direction is down for the inverted-U of the next higher level N=0,4,8,12.
This non-3 (or non whatever highest digit) is a general procedure and can be used on any state-based high-to-low procedure of self-similar curves. In the current code it's used to apply a fractional part of N in the correct direction but is not otherwise made directly available.
Because the Hilbert curve has no "reversal" sections it also works to build a direction from low to high N digits. 1 and 2 digits make no change to the orientation, 0 digit is a transpose, and a 3 digit is a rotate and transpose, except that low 3s are transpose-only (no rotate) for the same reason as taking the lowest non-3 above.
Jorg Arndt in the fxtbook above notes the direction can be obtained just by counting 3s in n and twos-complement -n. The only thing to note is that the numbering there starts n=1, unlike the PlanePath starting N=0, so it becomes
N+1 count 3s / 0 mod 2 S or E \ 1 mod 2 N or W -(N+1) count 3s / 0 mod 2 N or E \ 1 mod 2 S or W
For the twos-complement negation an even number of base-4 digits of N must be taken. Because -(N+1) = ~N, ie. a ones-complement, the second part is also
N count 0s / 0 mod 2 N or E [even num digits] \ 1 mod 2 S or W
Putting the two together then
N count 0s N+1 count 3s direction (0=E,1=N,etc) 0 mod 2 0 mod 2 0 1 mod 2 0 mod 2 3 0 mod 2 1 mod 2 1 1 mod 2 1 mod 2 2
This path is in Sloane's OEIS in many forms,
A059252 Y coord A059253 X coord A059261 X+Y A059285 X-Y A163547 X^2+Y^2 radius squared A139351 HammingDist(X,Y), count 1-bits at even positions in N A163365 sum N on diagonal A163477 sum N on diagonal, divided by 4 A163482 N values on X axis A163483 N values on Y axis A062880 N values on diagonal X=Y (digits 0,2 in base 4) A163538 dX -1,0,1 change in X A163539 dY -1,0,1 change in Y A163540 absolute direction of each step (0=E,1=S,2=W,3=N) A163541 absolute direction, swapped X,Y A163542 relative direction (ahead=0,right=1,left=2) A163543 relative direction, swapped X,Y A163900 distance dX^2+dY^2 between Hilbert and ZOrder A165464 distance dX^2+dY^2 between Hilbert and PeanoCurve A165465 N where Hilbert and Peano have same X,Y
The following take points of the plane in various orders, each value in the sequence being the N of the Hilbert curve at those positions.
A163355 N by the ZOrderCurve points sequence A163356 inverse, ZOrderCurve by Hilbert points order A166041 N by the PeanoCurve points sequence A166042 inverse, PeanoCurve N by Hilbert points order A163357 N by diagonals like Math::PlanePath::Diagonals with first Hilbert step along same axis the diagonals start A163358 inverse A163359 N by diagonals, transposed start along the opposite axis A163360 inverse A163361 A163357 + 1, numbering the Hilbert N's from N=1 A163362 inverse A163363 A163355 + 1, numbering the Hilbert N's from N=1 A163364 inverse
These sequences are permutations of the integers since all X,Y positions of the first quadrant are covered by each path (Hilbert, ZOrder, Peano). The inverse permutations can be thought of taking X,Y positions in the Hilbert order and asking what N the ZOrder, Peano or Diagonals path would put there.
The A163355 permutation by ZOrderCurve can be considered for repeats or cycles,
A163905 ZOrderCurve permutation A163355 applied twice A163915 ZOrderCurve permutation A163355 applied three times A163901 fixed points (N where X,Y same in both curves) A163902 2-cycle points A163903 3-cycle points A163890 cycle lengths, points by N A163904 cycle lengths, points by diagonals A163910 count of cycles in 4^k blocks A163911 max cycle length in 4^k blocks A163912 LCM of cycle lengths in 4^k blocks A163914 count of 3-cycles in 4^k blocks A163909 those counts for even k only A163891 N of previously unseen cycle length A163893 first differences of those A163891 A163894 smallest value not an n-cycle A163895 position of new high in A163894 A163896 value of new high in A163894 A163907 ZOrderCurve permutation twice, on points by diagonals A163908 inverse of this
See examples/hilbert-oeis.pl in the Math-PlanePath sources for a sample program printing the A163359 permutation values.
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
Copyright 2010, 2011, 2012, 2013, 2014 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/>.