Created
August 26, 2017 18:52
-
-
Save frostburn/8a1d0d8a6ff33622dcf749375baf49ef to your computer and use it in GitHub Desktop.
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 python3 | |
import argparse | |
BAILOUT = 16 | |
MAX_ITER = 56 | |
def escape_time(c): | |
z = 0j # Squaring accumulator | |
for iteration in range(MAX_ITER): | |
z = c + z*z | |
if z.real ** 2 + z.imag ** 2 > BAILOUT: | |
break | |
return iteration + 1 # Reproduces the bug in the Plorth version | |
def color_print(n): | |
print("\u001b[3{};1m{}".format(n % 8, n % 10), end="") | |
def render(width, height): | |
corner = -2.25 - 1.5j | |
x_step = 3 / width | |
y_step = 3j / height | |
for j in range(1, height): | |
for i in range(width): | |
c = corner + i * x_step + j * y_step | |
color_print(escape_time(c)) | |
print() | |
if __name__ == "__main__": | |
parser = argparse.ArgumentParser(description='Render the Mandelbrot fractal.') | |
parser.add_argument('--width', metavar='W', type=int, default=70, help='Width of the image') | |
parser.add_argument('--height', metavar='H', type=int, default=32, help='Height of the image') | |
args = parser.parse_args() | |
render(args.width, args.height) | |
print("\u001b[0m") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment