Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # tk_mandelbrot_autozoom.py
- # just further code experimenting
- from tkinter import *
- from random import choice
- import math
- ww = 200
- hh = 200
- w0 = ww // 2
- h0 = hh // 2
- root = Tk()
- canvas = Canvas(root, width=ww, height=hh)
- canvas.pack()
- def mandelbrot(c):
- z = 0
- for i in range(20):
- z = z**2 + c
- if abs(z) > 2:
- return i
- return 20
- def renderMandelbrot(xmin, xmax, ymin, ymax):
- canvas.delete('all')
- for x in range(ww):
- p = 0
- for y in range(hh):
- c = complex(x / ww * (xmax - xmin) + xmin, y / hh * (ymax - ymin) + ymin)
- try:
- val = val_dict[c]
- except:
- val = mandelbrot(c)
- val_dict[c] = val
- val = (val**3) % 256
- if val > 180 and p == 0:
- target.append((x, y))
- p = val
- color = f'#00{val:02x}00'
- canvas.create_line(x, y, x+1, y+1, fill=color)
- canvas.update()
- xmin = -2
- xmax = 2
- ymin = -2
- ymax = 2
- x_center = -0.8
- y_center = 0
- x = x_center
- y = y_center
- val_dict = {}
- i = 0.9
- while 1:
- i += 0.1
- zoom = math.exp(i / 20 * 5)
- x_range = (xmax - xmin) / zoom
- y_range = (ymax - ymin) / zoom
- xmin = x_center - x_range / 2
- xmax = x_center + x_range / 2
- ymin = y_center - y_range / 2
- ymax = y_center + y_range / 2
- target = []
- renderMandelbrot(xmin, xmax, ymin, ymax)
- if 10 < len(target) < 180:
- x, y = choice(target)
- else:
- x = ww*0.6 if x_center < 0 else ww*0.4
- y = hh*0.6 if y_center < 0 else hh*0.4
- i -= 0.025
- x_center = x / ww * (xmax - xmin) + xmin
- y_center = y / hh * (ymax - ymin) + ymin
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement