Created
June 17, 2017 22:00
-
-
Save a5an0/da069831197f7b76062dcdff4bf50dc8 to your computer and use it in GitHub Desktop.
convertCoords.py
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
#!/usr/bin/env python | |
def convertToLinear(x, y, z, xlen=10, ylen=10, zlen=10): | |
""" | |
Given a 0-indexed point in zig-zagging 3-space, convert it to an | |
index in a 0-indexed LED array | |
""" | |
assert x < xlen | |
assert y < ylen | |
assert z < zlen | |
linearCoord = 0 | |
linearCoord += (z * (xlen * ylen)) | |
if z % 2 == 0: | |
linearCoord += max(0, y * xlen) | |
if y % 2 == 0: | |
linearCoord += x | |
else: | |
linearCoord += (xlen - 1) - x | |
else: | |
linearCoord += max(0, ((ylen - 1) - y) * xlen) | |
if y % 2 == 0: | |
linearCoord += (xlen - 1) - x | |
else: | |
linearCoord += x | |
return max(0, linearCoord) | |
def main(): | |
def c3(x,y,z): | |
return convertToLinear(x,y,z,3,3,3) | |
assert c3(0,0,0) == 0 | |
assert c3(1,1,1) == 13 | |
assert c3(2,2,2) == 26 | |
assert c3(0,0,2) == 18 | |
assert c3(1,1,1) == 13 | |
assert c3(2,1,1) == 14 | |
print("all good") | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment