0

I am trying to write a function that calculates the distance along a point appears along a 3-Dimensional Hilbert Curve. Essentially a function that can take in the x, y, z coordinates of a point and calculate where on the curve it appears. Assume x, y, and z can be integers 0 - 255, roughly corresponding to the RGB colorspace. This way I can create an ordered list of points based on the Hilbert Curve.

I have tried implementing code given here on stack overflow, however this gets me stuck in a recursive loop when I try to write it in C#, furthermore isn't quite what I am after. I have also tried implementing this code, however, I must misunderstand it, because it gives me what appears to be entirely randomized results.

Currently, I am following this guide, and have gotten through coding up the section when it comes to grey-code. However, I am stuck when it comes to the example calculation with rotation. In the table, I am not sure how the final number is being produced between the chnk, rotation, and flipbit.

I am a computer science student with not much of a mathematical background.

Below is the code sample I have so far

    //This might be a little more than graycode??
    public long GreyCode()
    {
        //convert the integers in the point to a binary representation of them
        string bx = Convert.ToString(Convert.ToInt32(x), 2).PadLeft(16, '0');
        string by = Convert.ToString(Convert.ToInt32(y), 2).PadLeft(16, '0');
        string bz = Convert.ToString(Convert.ToInt32(z), 2).PadLeft(16, '0');

        //store the binary values in an array to iterate over
        int[] bxArr = bx.Select(c => c - '0').ToArray();
        int[] byArr = by.Select(c => c - '0').ToArray();
        int[] bzArr = bz.Select(c => c - '0').ToArray();

        //call a function that will iterate over each one xoring every bit together
        //Xor's each bit by it's neighboring bit to the right 
        //until it reaches the end of the array
        bxArr = XorArray(bxArr);
        byArr = XorArray(byArr);
        bzArr = XorArray(bzArr);

        //This is a magic number and I don't understand it tbh
        int rank = 4;

        //Using rank, combine the arrays and flip every rankth bit
        int[] combined = (bxArr.Concat(byArr).ToArray()).Concat(bzArr).ToArray();
        int[] bitArr = FlipBits(rank, combined);

        //rotations and stuff

        //finally, convert to integer and store
        string resStr = string.Join("", bitArr);
        long ret = Convert.ToInt64(resStr, 2);

        return ret;
    }
Dan Karbayev
  • 2,870
  • 2
  • 19
  • 28
  • 1
    To correct one of your points: you cannot measure a distance along a Hilbert curve, because such curves are not rectifiable. To cover all points in space, there is an infinite distance (or no distance) between two distinct points on the curve. However, such a curve is a function from the unit interval to points in space, so one can speak of distances along the unit interval. So are you asking for a function from the `x,y,z` of a point on the curve back to a point in the unit interval? Also be aware that the curve is not one-to-one so there may be many such points in the unit interval. – Rory Daulton Nov 29 '18 at 21:37
  • 1
    Another possibility is that you mean an *approximation* to the Hilbert curve. That is what is mean in your first link. In that case you can write of a distance along the curve. Please clarify what you mean. And if you mean the approximation, state how many points along each dimension your want to cover with the curve. – Rory Daulton Nov 29 '18 at 21:58
  • 1
    I do mean approximation, I tried to clarify the question as best I could, where x, y, and z can be integers between 0 - 255, but I would like to understand the problem enough to generalize this for different integers in the future. Let me know if I understood your question. – C. Dumbluck Nov 29 '18 at 22:14

0 Answers0