Function hilbert_2d::u16::h2xy_discrete
source · Expand description
Maps from a 1D index to a 2D coordinate, using a discrete approximation of the Hilbert curve. Recommended for images and matrices.
Given h
, this method calculates the (x, y)
coordinates for that index, in the
Hilbert curve approximation of order
.
The value of h
must be in the range 0 <= h < 2^(2 * order)
. The coordinates
returned will be in the range 0 <= x/y < 2^order
.
With b
being the number of bits in a u16
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, u16::h2xy_discrete};
// Hilbert curve of order 2:
// 5 ―― 6 9 ― 10
// | | | |
// 4 7 ―― 8 11
// | |
// 3 ―― 2 13 ― 12
// | |
// 0 ―― 1 14 ― 15
let (x, y) = h2xy_discrete(7, 2, Variant::Hilbert);
assert_eq!(x, 1);
assert_eq!(y, 2);