here2share

# Tk_spectral_fit_mandelbrot.py

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