public static long HilbertDistance(float latitude, float longitude, long n) { long x = (long)(((double)longitude + 180.0) / 360.0 * (double)n); if (x >= n) { x = n - 1L; } long y = (long)(((double)latitude + 90.0) / 180.0 * (double)n); if (y >= n) { y = n - 1L; } return(HilbertCurve.xy2d(n, x, y)); }
// DISCLAIMER: some of this stuff is straight from wikipedia: // http://en.wikipedia.org/wiki/Hilbert_curve#Applications_and_mapping_algorithms /// <summary> /// Calculates hilbert distance. /// </summary> /// <returns></returns> public static long HilbertDistance(float latitude, float longitude, long n) { // calculate x, y. var x = (long)((((double)longitude + 180.0) / 360.0) * n); if (x >= n) { x = n - 1; } var y = (long)((((double)latitude + 90.0) / 180.0) * n); if (y >= n) { y = n - 1; } // calculate hilbert value for x-y and n. return(HilbertCurve.xy2d(n, x, y)); }