Last active
August 29, 2015 14:00
-
-
Save ashwin/11204582 to your computer and use it in GitHub Desktop.
Demo of Morton index in 2D
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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