Skip to content

Instantly share code, notes, and snippets.

@ahihi
Last active August 29, 2015 14:23
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 ahihi/07e900c26737ad262baf to your computer and use it in GitHub Desktop.
Save ahihi/07e900c26737ad262baf to your computer and use it in GitHub Desktop.
Rainbow Sierpinski triangles
USE_256 = True
SYMBOL = "✿" # change if your font lacks the flower :(
import itertools
import shutil
import sys
import time
if USE_256: c, p = (9, 208, 11, 64, 27, 5), "38;5;"
else: c, p = (41, 43, 42, 46, 44, 45), ""
w = shutil.get_terminal_size()[0]
n1 = w//2 + 1
n2 = w - n1 - 2
x = n1*[0] + [1] + n2*[0]
get = lambda i: x[i] if 0 <= i < len(x) else 0
ns = lambda i: get(i-1) + get(i+1)
for l in itertools.count():
for k in range(len(x)):
if x[k]: print("\033[" + p + str(c[((l//4+k)//6) % len(c)]) + "m" + SYMBOL + "\033[0m", end="")
else: print(" ", end="")
print()
x = [int(ns(i) == 1) for i in range(len(x))]
time.sleep(0.1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment