Skip to content

Instantly share code, notes, and snippets.

@prophile
Created March 5, 2012 18:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save prophile/b228f8b37dfd1bc634f1 to your computer and use it in GitHub Desktop.
Save prophile/b228f8b37dfd1bc634f1 to your computer and use it in GitHub Desktop.
class SquareSpiral:
_basic_map = {(0, 0): False,
(0, 1): True,
(1, 0): True,
(1, 1): True}
def __init__(self, w, h):
assert (w - 2) % 4 == 0
assert (h - 2) % 4 == 0
assert w == h
self.w = w
self.h = h
if w > 2 and h > 2:
self.inner = SquareSpiral(w - 4, h - 4)
else:
self.inner = None
def black_at(self, x, y):
if self.w == 2 and self.h == 2:
return self._basic_map[(x, y)]
else:
# Detect the borders
if y == self.h - 1:
return True
if x == self.w - 1:
return True
if y == 0:
return x > 0
if x == 0:
return False
if y == self.h - 2:
return False
if x == self.w - 2:
return False
if y == 1:
return x == 1
if x == 1:
return True
return self.inner.black_at(x - 2, y - 2)
ss = SquareSpiral(34, 34)
for y in xrange(34):
for x in xrange(34):
print '#' if ss.black_at(x, 33 - y) else ' ',
print ''
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment