Created
May 13, 2012 15:57
-
-
Save foota/2689040 to your computer and use it in GitHub Desktop.
Tetration fractal (Python)
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
#!/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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
http://handasse.blogspot.com/2012/05/blog-post.html