Skip to content

Instantly share code, notes, and snippets.

@ashwin
Last active August 29, 2015 14:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ashwin/11204582 to your computer and use it in GitHub Desktop.
Save ashwin/11204582 to your computer and use it in GitHub Desktop.
Demo of Morton index in 2D
#include <stdint.h>
#include <cstdio>
uint32_t make_morton(uint32_t x, uint32_t y)
{
uint32_t m = 0;
for (int bit = 31; bit >= 0; --bit)
{
uint32_t mask = 1 << bit;
uint32_t xbit = (x & mask) >> bit;
uint32_t ybit = (y & mask) >> bit;
m = (m << 1) | xbit;
m = (m << 1) | ybit;
}
return m;
}
int main()
{
int32_t xlen;
int32_t ylen;
printf("Enter width: ");
scanf("%d", &xlen);
printf("Enter height: ");
scanf("%d", &ylen);
printf("\n\nMorton curve:\n");
for (int32_t y = ylen - 1; y >= 0; --y)
{
for (int32_t x = 0; x < xlen; ++x)
{
uint32_t m = make_morton(x, y);
printf("%5u ", m);
}
printf("\n");
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment