Last active
March 16, 2024 19:52
-
-
Save ExFed/15c7881bdc240da35386464b8a8c5c86 to your computer and use it in GitHub Desktop.
Z-Order Curve
This file contains hidden or 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
#!/usr/bin/env groovy | |
ztoc = { int d, int z -> | |
if (0 == d) { | |
return [] | |
} | |
if (1 == d) { | |
return [z] | |
} | |
def m = 1 | |
def c = [0]*d | |
while (z) { | |
for (int i = 0; i < c.size(); i++) { | |
c[i] |= m & z | |
z >>= 1 | |
} | |
z <<= 1 | |
m <<= 1 | |
} | |
return c | |
} | |
ctoz = { List coord -> | |
def c = [] + coord | |
def z = 0 | |
def k = 0 | |
while (c && c.any()) { | |
for (int i = 0; i < c.size(); i++) { | |
z |= (1 & c[i]) << k | |
c[i] >>= 1 | |
k++ | |
} | |
} | |
return z | |
} | |
switch (args[0]) { | |
case 'ztoc' -> println ztoc(args[1] as int, args[2] as int).join(' ') | |
case 'ctoz' -> println ctoz(args[1..-1].collect { it as int }) | |
default -> assert false | |
} |
This file contains hidden or 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
#!/usr/bin/env python3 | |
import sys | |
def ztoc(d, z): | |
if 0 == d: | |
return [] | |
if 1 == d: | |
return [z] | |
m = 1 | |
c = [0]*d | |
while z: | |
for i in range(len(c)): | |
c[i] |= m & z | |
z >>= 1 | |
z <<= 1 | |
m <<= 1 | |
return c | |
def ctoz(c): | |
c = c.copy() | |
z = 0 | |
k = 0 | |
while c and any(c): | |
for i in range(len(c)): | |
z |= (1 & c[i]) << k | |
c[i] >>= 1 | |
k += 1 | |
return z | |
if __name__ == "__main__": | |
args = sys.argv[1:] | |
if 'ztoc' == args[0]: | |
print(ztoc(int(args[1]), int(args[2]))) | |
elif 'ctoz' == args[0]: | |
print(ctoz([int(i) for i in args[1:]])) | |
else: | |
assert False |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment