Created
July 1, 2013 18:03
-
-
Save bgr/5903105 to your computer and use it in GitHub Desktop.
draws Sierpinski triangle
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
""" | |
Draws a Sierpinski triangle (using PIL library) | |
result (using 2**13 rows = 8192 x 8192): http://i.imgur.com/qFkI9Yu.png | |
""" | |
rows_to_write = 2**11 | |
def gen_rows(n_rows): | |
r = 1 | |
for i in range(n_rows): | |
yield r | |
# python handles big integers, no need for string version | |
r = (r << 1) ^ r | |
int_rows = gen_rows(rows_to_write) | |
rows = [bin(r)[2:] for r in int_rows] | |
max_width = max([len(r) for r in rows]) | |
centered = [r.center(max_width, '0') for r in rows] | |
colors = { | |
'1': 0x0, | |
'0': 0xFFFFFF, | |
} | |
pixels = [colors[px] for row in centered for px in row] | |
from PIL import Image | |
img = Image.new('RGB', (max_width, rows_to_write)) | |
img.putdata(pixels) | |
img.save('sierpinski.png') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment