Skip to content

Instantly share code, notes, and snippets.

@foota
Created June 21, 2017 09:43
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 foota/862d94e99c93958ab0a654b14f2d2866 to your computer and use it in GitHub Desktop.
Save foota/862d94e99c93958ab0a654b14f2d2866 to your computer and use it in GitHub Desktop.
Tetration fractal (Python 3)
#!/usr/bin/env python
import sys
import os
import math
import 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 * 0.5
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("Usage: %s imagefile left top width height [screen_width=512 screen_height=512 max_iters=256]" % os.path.basename(args[0]), file=sys.stderr)
print(" Ex. %s tetration.png -1.5 0.0 0.75 0.75" % os.path.basename(args[0]), file=sys.stderr)
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: %.2f" % (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