Skip to content

Instantly share code, notes, and snippets.

@apg
Last active August 29, 2015 14:07
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 apg/0ac682c18d3a852fefcd to your computer and use it in GitHub Desktop.
Save apg/0ac682c18d3a852fefcd to your computer and use it in GitHub Desktop.
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