Advertisement
here2share

# Tk_mandelbrot4.py

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