Skip to content

Instantly share code, notes, and snippets.

@itsjohncs
Created December 1, 2016 08:11
Show Gist options
  • Save itsjohncs/13fbd39c500f04796803a582add1a92f to your computer and use it in GitHub Desktop.
Save itsjohncs/13fbd39c500f04796803a582add1a92f to your computer and use it in GitHub Desktop.
CHUNK_WIDTH = 32
class Point(object):
def __init__(self, x, y, z):
self.x = x
self.y = y
self.z = z
def to_index(self):
return point.x + point.y * CHUNK_WIDTH + point.z * CHUNK_WIDTH * CHUNK_WIDTH
@classmethod
def from_index(cls, index):
return cls(
index % CHUNK_WIDTH,
(index // CHUNK_WIDTH) % CHUNK_WIDTH,
index // (CHUNK_WIDTH * CHUNK_WIDTH),
)
def __repr__(self):
return "Point(x=%s, y=%s, z=%s)" % (self.x, self.y, self.z)
def __equals__(self, other):
return self.x == other.x and self.y == other.y and self.z == other.z
def __hash__(self):
return hash((self.x, self.y, self.z))
for index in xrange(CHUNK_WIDTH ** 3):
point = Point.from_index(index)
assert point.to_index() == index
assert len({Point.from_index(index) for index in xrange(CHUNK_WIDTH ** 3)}) == CHUNK_WIDTH ** 3
print "Good"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment