Advertisement
here2share

# tk_mandelbrot_autozoom.py

Aug 18th, 2023
1,087
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.53 KB | None | 0 0
  1. # tk_mandelbrot_autozoom.py
  2.  
  3. # just further code experimenting
  4.  
  5. from tkinter import *
  6. from random import choice
  7. import math
  8.  
  9. ww = 200
  10. hh = 200
  11. w0 = ww // 2
  12. h0 = hh // 2
  13.  
  14. root = Tk()
  15. canvas = Canvas(root, width=ww, height=hh)
  16. canvas.pack()
  17.  
  18. def mandelbrot(c):
  19.     z = 0
  20.     for i in range(20):
  21.         z = z**2 + c
  22.         if abs(z) > 2:
  23.             return i
  24.     return 20
  25.  
  26. def renderMandelbrot(xmin, xmax, ymin, ymax):
  27.     canvas.delete('all')
  28.     for x in range(ww):
  29.         p = 0
  30.         for y in range(hh):
  31.             c = complex(x / ww * (xmax - xmin) + xmin, y / hh * (ymax - ymin) + ymin)
  32.             try:
  33.                 val = val_dict[c]
  34.             except:
  35.                 val = mandelbrot(c)
  36.                 val_dict[c] = val
  37.             val = (val**3) % 256
  38.             if val > 180 and p == 0:
  39.                 target.append((x, y))
  40.                 p = val
  41.             color = f'#00{val:02x}00'
  42.             canvas.create_line(x, y, x+1, y+1, fill=color)
  43.     canvas.update()
  44.  
  45. xmin = -2
  46. xmax = 2
  47. ymin = -2
  48. ymax = 2
  49. x_center = -0.8
  50. y_center = 0
  51. x = x_center
  52. y = y_center
  53. val_dict = {}
  54.  
  55. i = 0.9
  56. while 1:
  57.     i += 0.1
  58.     zoom = math.exp(i / 20 * 5)
  59.     x_range = (xmax - xmin) / zoom
  60.     y_range = (ymax - ymin) / zoom
  61.    
  62.     xmin = x_center - x_range / 2
  63.     xmax = x_center + x_range / 2
  64.     ymin = y_center - y_range / 2
  65.     ymax = y_center + y_range / 2
  66.    
  67.     target = []
  68.     renderMandelbrot(xmin, xmax, ymin, ymax)
  69.     if 10 < len(target) < 180:
  70.         x, y = choice(target)
  71.     else:
  72.         x = ww*0.6 if x_center < 0 else ww*0.4
  73.         y = hh*0.6 if y_center < 0 else hh*0.4
  74.         i -= 0.025
  75.     x_center = x / ww * (xmax - xmin) + xmin
  76.     y_center = y / hh * (ymax - ymin) + ymin
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement