2

I am building a quadtree using the techniques described in this wikipedia article. I am storing the coordinates in an 2- or 3-dimansional array.

boost::array<unsigned int, 2 /* 3 */> coord;

I need a method to calculate the coordinates of the next box in z-order. It would work by interleaving the bits, increasing by one and than deinterleaving, but this gets very complicated. I hope there is an implementation similar to the cmp_zorder(...) methon in the article wich works without interleaving the bits.

Smittii
  • 187
  • 1
  • 10
  • What exactly do you want? The (x', y') that is next to (x, y) in Z-order, but without interleaving and de-interleaving? – harold May 26 '12 at 20:31
  • Exactly right, is it possible? – Smittii May 27 '12 at 07:40
  • Certainly, but I haven't heard (and can't find) any tricks, so the only way I can think of is modifying the bitwise addition algorithm to add the carries to the other coordinate (carries in x should be added to y, and carries in y should be added to x). A similar technique could be used for the 3D Z-curve. That doesn't sound particularly fast to me though. Are you interested in that way anyway? – harold May 27 '12 at 08:58
  • yes, i'm intrested, sounds like a lot of bit operations – Smittii May 27 '12 at 16:28

1 Answers1

1

Ok here's the "mutilated" addition algorithm, x and y are inputs as well as outputs, and it is assumed that the lowest order bit in the interleaved coordinate would be x (the same as in the wikipedia article)

int carry = 1;
do
{
    int newcarry = x & carry;
    x ^= carry;
    carry = newcarry;
    newcarry = (y & carry) << 1;
    y ^= carry;
    carry = newcarry;
} while (carry != 0);

I did test it, but only a little.

harold
  • 61,398
  • 6
  • 86
  • 164