// 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> /// <param name="latitude">The latitude.</param> /// <param name="longitude">The longitude.</param> /// <param name="n">The accuracy, used to divide the lat/lon space.</param> /// <returns></returns> public static ulong HilbertDistance(float latitude, float longitude, int n) { // calculate x, y. ulong x = (ulong)(((longitude + 180) / 360.0) * n); ulong y = (ulong)(((latitude + 90) / 180.0) * n); // calculate hilbert value for x-y and n. return(HilbertCurve.xy2d(n, x, y)); }
private void GenerateLayout() { int initialD = HilbertCurve.xy2d(n, initialHilbertTile.x, initialHilbertTile.y); int i = initialD; int maxSteps = n * n - 1; Connections direction = Connections.None; List <List <int2> > unconnectedPaths = new List <List <int2> >(); List <int2> currentPath = new List <int2>(); int2 currentTile = new int2(initialHilbertTile.x, initialHilbertTile.y); currentPath.Add(Hilbert2Layout(currentTile)); while (i < maxSteps) { int2 nextTile = HilbertCurve.d2xy(n, i + 1); if (IsInsideOffsetFrame(nextTile.x, nextTile.y)) { direction = GetDirection(currentTile, nextTile); if (direction != Connections.None) // we can connect currentTile and nextTile directly. { int2 layoutTile = Hilbert2Layout(currentTile); layoutConnections[layoutTile.x, layoutTile.y] |= direction; } else { unconnectedPaths.Add(currentPath); currentPath = new List <int2>(); } currentPath.Add(Hilbert2Layout(nextTile)); currentTile = nextTile; } i++; } unconnectedPaths.Add(currentPath); ConnectUnconnectedPaths(unconnectedPaths); exit = currentTile; }