Skip to content

Instantly share code, notes, and snippets.

@Shikugawa
Created January 2, 2021 08:07
Show Gist options
  • Save Shikugawa/7d5073a8efc4fc01ba5c43e6ed054a2e to your computer and use it in GitHub Desktop.
Save Shikugawa/7d5073a8efc4fc01ba5c43e6ed054a2e to your computer and use it in GitHub Desktop.
4分木分割の場合のムートン座標を求めるプログラム
int main() {
int width = 640;
int height = 640;
int morton_unit_x = width / 8;
int morton_unit_y = height / 8;
double x = 533.2;
double y = 315.8;
uint16_t morton_x = std::floor(x / morton_unit_x);
uint16_t morton_y = std::floor(y / morton_unit_y);
auto separatebit = [](uint16_t n) -> uint32_t {
n = (n | (n << 8)) & 0x00ff00ff;
n = (n | (n << 4)) & 0x0f0f0f0f;
n = (n | (n << 2)) & 0x33333333;
return (n | (n << 1)) & 0x55555555;
};
// morton_x, yの処理
// std::cout << morton_x << std::endl;
// int morton_x2 = (morton_x | (morton_x << 2)) & 0x33;
//// printf("%d\n", morton_x2);
// int morton_y2 = (morton_y | (morton_y << 2)) & 0x33;
//// printf("%d\n", morton_y2);
uint32_t region_number = separatebit(morton_x) | (separatebit(morton_y) << 1);
printf("%d\n", region_number);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment