private static long xy2d(long n, long x, long y) { long num = 0; long n1 = n / 2L; while (n1 > 0L) { long rx = (x & n1) > 0L ? 1L : 0L; long ry = (y & n1) > 0L ? 1L : 0L; num += n1 * n1 * (3L * rx ^ ry); HilbertCurve.rot(n1, ref x, ref y, rx, ry); n1 /= 2L; } return(num); }
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)); }
private static void d2xy(long n, long d, out long x, long y) { long num = d; x = y = 0L; long n1 = 1; while (n1 < n) { long rx = 1L & num / 2L; long ry = 1L & (num ^ rx); HilbertCurve.rot(n1, ref x, ref y, rx, ry); x = x + n1 * rx; y += n1 * ry; num /= 4L; n1 *= 2L; } }
// 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)); }
/// <summary> /// Calculates all distinct hilbert distances inside of the given bounding box. /// </summary> /// <returns></returns> public static List <long> HilbertDistances(float minLatitude, float minLongitude, float maxLatitude, float maxLongitude, long n) { var deltaLat = 180.0f / n; var deltaLon = 360.0f / n; var distances = new List <long>((int)( ((maxLatitude - minLatitude) / deltaLat) * ((maxLongitude - minLongitude) / deltaLon))); minLatitude = System.Math.Max(minLatitude - deltaLat, -90); minLongitude = System.Math.Max(minLongitude - deltaLon, -180); maxLatitude = System.Math.Min(maxLatitude + deltaLat, 90); maxLongitude = System.Math.Min(maxLongitude + deltaLon, 180); for (var latitude = minLatitude; latitude < maxLatitude; latitude = latitude + deltaLat) { for (var longitude = minLongitude; longitude < maxLongitude; longitude = longitude + deltaLon) { distances.Add(HilbertCurve.HilbertDistance(latitude, longitude, n)); } } return(distances); }
public static List <long> HilbertDistances(float minLatitude, float minLongitude, float maxLatitude, float maxLongitude, long n) { float num1 = 180f / (float)n; float num2 = 360f / (float)n; List <long> longList = new List <long>((int)(((double)maxLatitude - (double)minLatitude) / (double)num1 * (((double)maxLongitude - (double)minLongitude) / (double)num2))); minLatitude = System.Math.Max(minLatitude - num1, -90f); minLongitude = System.Math.Max(minLongitude - num2, -180f); maxLatitude = System.Math.Min(maxLatitude + num1, 90f); maxLongitude = System.Math.Min(maxLongitude + num2, 180f); float latitude = minLatitude; while ((double)latitude < (double)maxLatitude) { float longitude = minLongitude; while ((double)longitude < (double)maxLongitude) { longList.Add(HilbertCurve.HilbertDistance(latitude, longitude, n)); longitude += num2; } latitude += num1; } return(longList); }