Create a gist now

Instantly share code, notes, and snippets.

@apg /fractal.py
Last active Aug 29, 2015

What would you like to do?
import re, itertools
from PIL import Image
def str2pt(s):
L = len(s) - 1
x, y = 1, 1
for n in s:
y += 2**L if n in '34' else 0
x += 2**L if n in '14' else 0
L -= 1
return x-1, y-1
def generate(n):
return map(lambda x: ''.join(x), itertools.product('1234', repeat=n))
def fractal(r, n):
return map(str2pt, (c for c in generate(n) if re.match(r, c)))
def draw(r, n=10, filename='/tmp/fractal.png'):
im = Image.new('RGB', (2**n, 2**n), "black")
for x, y in fractal(r, n):
im.putpixel((x, y), (255, 255, 255,))
im.save(filename, 'PNG')
draw('.*1.*')

Produces:

.1.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment