Skip to content

Instantly share code, notes, and snippets.

@nanki
Created April 13, 2013 14:58
Show Gist options
  • Save nanki/5378736 to your computer and use it in GitHub Desktop.
Save nanki/5378736 to your computer and use it in GitHub Desktop.
render mandelbrot set on terminal
import sys
import math
def dot(bits):
bits = bits & 7 | (bits & 112) >> 1 | (bits & 8) << 3 | bits & 128
return unichr(0x2800 + bits)
def mandelbrot(x, y, r, iteration=1000):
xx = 1.4 * float(x - 1.4 * r) / r
yy = 1.4 * float(y - r) / r
c = complex(xx, yy)
r = c
for i in range(iteration):
r = r * r + c
if abs(r) > 2:
return False
return True
if len(sys.argv) > 1:
R = int(sys.argv[1])
else:
R = 90
for y in range(R/2):
for x in range(R):
r = reduce(lambda r, i: r | mandelbrot(x * 2 + ((i & 4) >> 2), y * 4 + (i & 3), R) << i, range(8), 0)
sys.stdout.write(dot(r).encode('utf8'))
print
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment