Skip to content

Instantly share code, notes, and snippets.

@foota
Created May 13, 2012 15:57
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save foota/2689040 to your computer and use it in GitHub Desktop.
Save foota/2689040 to your computer and use it in GitHub Desktop.
Tetration fractal (Python)
#!/usr/bin/env python
import sys, os, math, time
from PIL import Image
def tetration(l, t, w, h, sw, sh, mit):
data = []
for ky in range(sh):
for kx in range(sw):
x = l + w * kx / (sw - 1)
y = t + h * ky / (sh - 1)
for i in range(mit):
try:
e = math.exp(-0.5 * math.pi * y)
p = math.pi * x / 2
x = e * math.cos(p)
y = e * math.sin(p)
except:
break
if math.hypot(x, y) > 1000000:
break
data.append((i % 4 * 64, i % 8 * 32, i % 16 * 16))
return data
def main(args):
if len(args) < 6:
print >>sys.stderr, "Usage: %s imagefile left top width height [screen_width=512 screen_height=512 max_iters=256]" % os.path.basename(args[0])
print >>sys.stderr, " Ex. %s tetration.png -1.5 0.0 0.75 0.75" % os.path.basename(args[0])
sys.exit(1)
imagefile = args[1]
l, t, w, h = map(float, args[2:6])
sw, sh, mit = 512, 512, 256
if len(args) >= 7: sw = int(args[6])
if len(args) >= 8: sh = int(args[7])
if len(args) >= 9: mit = int(args[8])
print "Range: (%f, %f) - (%f, %f)" % (l, t, l + w, t + h)
print "Image: %d x %d" % (sw, sh)
print "Max number of iterations: %d" % mit
start_time = time.time()
data = tetration(l, t, w, h, sw, sh, mit)
print "Time: %f" % (time.time() - start_time)
im = Image.new("RGB", (sw, sh))
for i, rgb in enumerate(data):
im.putpixel((i % sw, i / sh), rgb)
im.show()
im.save(imagefile)
if __name__ == "__main__": main(sys.argv)
@foota
Copy link
Author

foota commented May 14, 2012

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