Function hilbert_2d::u8::xy2h_discrete
source · Expand description
Maps from a 2D coordinate to an 1D index, using a discrete approximation of the Hilbert curve. Recommended for images and matrices.
Given x
and y
, this method calculates the h
index for that coordinate, in the
Hilbert curve approximation of order
.
The value of x
and y
must be in the range 0 <= x/y < 2^order
. The index
returned will be in the range 0 <= h < 2^(2 * order)
.
With b
being the number of bits in a u8
for the target platform, the highest approximation order achievable
by this method is b / 2
. Therefore, the parameter order
must be in the range
1 <= order <= b / 2
.
The pattern of the Hilbert curve to be constructed must be indicated by the
variant
parameter. See Variant
for more details.
Examples
Basic usage:
use hilbert_2d::{Variant, u8::xy2h_discrete};
// Hilbert curve of order 2:
// 5 ―― 6 9 ― 10
// | | | |
// 4 7 ―― 8 11
// | |
// 3 ―― 2 13 ― 12
// | |
// 0 ―― 1 14 ― 15
let h = xy2h_discrete(2, 1, 2, Variant::Hilbert);
assert_eq!(h, 13);