Skip to content

Instantly share code, notes, and snippets.

@alexanderkiel
Created September 25, 2019 20:36
Show Gist options
  • Save alexanderkiel/f95a6d95d20071170d542850bdb945a6 to your computer and use it in GitHub Desktop.
Save alexanderkiel/f95a6d95d20071170d542850bdb945a6 to your computer and use it in GitHub Desktop.
import timeit
from array import array
def c_add(c1, c2):
r1, i1 = c1
r2, i2 = c2
return (r1 + r2, i1 + i2)
def c_square(c):
r, i = c
return (r*r - i*i, 2*r*i)
def zn(n, c):
if n == 0:
return (0.0,0.0)
return c_add(c, c_square(zn(n-1, c)))
def abs_sq(c):
r, i = c
return r*r + i*i
def diverges(c):
return abs_sq(c) > 4.0
screen_width = 120
screen_height = 40
screen = [[1]]
def get_pixel(x, y):
return screen[y][x]
def set_pixel(x, y, v):
global screen
screen[y][x] = v
def print_screen():
for line in screen:
print("".join(map(str,line)))
scale_x = lambda x: x*2.5 / screen_width - 2.0
scale_y = lambda y: y*2.0 / screen_height - 1.0
def f(print=False):
global screen
screen = [None]*screen_height
for y in range(screen_height):
screen[y] = array('h', [1]*screen_width)
for n in range(1,20):
for x in range(screen_width):
for y in range(screen_height):
if get_pixel(x,y) and diverges(zn(n, (scale_x(x), scale_y(y)))):
set_pixel(x,y,0)
if print:
print_screen()
f(True)
print(timeit.timeit("f()", "from __main__ import f", number=20)/20)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment