Advertisement
here2share

# Tk_mandelbrot2.py

Jan 1st, 2021
987
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.50 KB | None | 0 0
  1. # Tk_mandelbrot2.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. #ff0000
  43. #ffa500
  44. #ffff00
  45. #008000
  46. #0000ff
  47. #4b0082
  48. #ee82ee
  49. '''.strip().split()
  50. Lr = len(rainbow)
  51.  
  52. up = 9.08373867835
  53. zz = 6.86105967195
  54. zz = Lr / (up + zz)
  55.  
  56. for row in range(hh):
  57.     for col in range(ww):
  58.         x = minX + col * xRange / ww
  59.         y = maxY - row * yRange / hh
  60.         oldX = x
  61.         oldY = y
  62.         for i in range(precision + 1):
  63.             a = x*x - y*y #real component of z^2
  64.             b = 2 * x * y #imaginary component of z^2
  65.             x = a + oldX #real component of new z
  66.             y = b + oldY #imaginary component of new z
  67.             if x*x + y*y > 4:
  68.                 break
  69.         if i < precision:
  70.             j = int(abs(oldX-x)+abs(oldY-y))
  71.  
  72.             try:
  73.                 rgb = rainbow[j]
  74.             except:
  75.                 rgb = rainbow[-1]
  76.             canvas.create_oval((col,row,col+1,row+1),outline=rgb)
  77.         else:
  78.             canvas.create_oval((col,row,col+1,row+1),outline='black')
  79.         index = row * ww + col + 1
  80.         # print("{} / {}, {}%".format(index, ww * hh, round(index / ww / hh * 100 * 10) / 10))
  81.     canvas.update()
  82.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement