here2share

# Tk_glassy_mandelbrot.py

Jan 1st, 2021 (edited)
525
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.75 KB | None | 0 0
  1. # Tk_glassy_mandelbrot.py
  2.  
  3. import math
  4.  
  5. #frame parameters
  6. ww = 600
  7. hh = 600
  8.  
  9. try:
  10.     import tkinter as tk
  11. except:
  12.     import Tkinter as tk
  13.  
  14. root=tk.Tk()
  15. root.title("Tk Glassy Mandelbrot")
  16.  
  17. xc=ww/2
  18. yc=hh/2
  19.  
  20. canvas=tk.Canvas(root,width=ww,height=hh)
  21. canvas.pack(fill="both",expand=True)
  22.  
  23. x = -0.65
  24. y = 0
  25. xRange = 3.4
  26. aspectRatio = 4/3
  27.  
  28. precision = 500
  29.  
  30. yRange = xRange / aspectRatio
  31. minX = x - xRange / 2
  32. maxX = x + xRange / 2
  33. minY = y - yRange / 2
  34. maxY = y + yRange / 2
  35.  
  36. def rgb2hex(rgb):
  37.     r,g,b = rgb
  38.     return "#{:02x}{:02x}{:02x}".format(r,g,b)
  39.  
  40. rainbow=[]
  41. def z(r,g,b):
  42.     rainbow.append(rgb2hex((r,g,b)))
  43. r,g,b=255,0,0
  44. for g in range(256):
  45.     z(r,g,b)
  46. for r in range(254, -1, -1):
  47.     z(r,g,b)
  48. for b in range(256):
  49.     z(r,g,b)
  50. for g in range(254, -1, -1):
  51.     z(r,g,b)
  52. for r in range(256):
  53.     z(r,g,b)
  54. for b in range(254, -1, -1):
  55.     z(r,g,b)
  56. Lr = len(rainbow)
  57.  
  58. up = 9.08373867835
  59. zz = 6.86105967195
  60. zz = Lr / (up + zz)
  61.  
  62. for row in range(hh):
  63.     for col in range(ww):
  64.         x = minX + col * xRange / ww
  65.         y = maxY - row * yRange / hh
  66.         oldX = x
  67.         oldY = y
  68.         for i in range(precision + 1):
  69.             a = x*x - y*y #real component of z^2
  70.             b = 2 * x * y #imaginary component of z^2
  71.             x = a + oldX #real component of new z
  72.             y = b + oldY #imaginary component of new z
  73.             if x*x + y*y > 4:
  74.                 break
  75.         if i < precision:
  76.             j = int((abs(xc-x)**2+abs(yc-y)**2)**0.5*50)
  77.             j += int((abs(xc+-row)**2+abs(yc+400-col)**2)**0.5)*5
  78.  
  79.             rgb = rainbow[(j+row*5)%Lr]
  80.  
  81.             canvas.create_oval((col,row,col+1,row+1),outline=rgb)
  82.         else:
  83.             canvas.create_oval((col,row,col+1,row+1),outline='black')
  84.         index = row * ww + col + 1
  85.         # print("{} / {}, {}%".format(index, ww * hh, round(index / ww / hh * 100 * 10) / 10))
  86.     canvas.update()
  87.  
Add Comment
Please, Sign In to add comment