Function hilbert_2d::u128::xy2h_discrete

source ·
pub const fn xy2h_discrete(x: u128, y: u128, order: u128, variant: Variant) -> u128
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 u128 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, u128::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);