Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Tk_Cache_Mandelbrot.py
- from tkinter import *
- from functools import lru_cache
- from PIL import Image, ImageTk
- ww = 200
- hh = 200
- root = Tk()
- root.title("Tk_Cache_Mandelbrot")
- root.geometry('{}x{}'.format(ww * 2, hh))
- canvas_main = Canvas(root, width=ww, height=hh)
- canvas_main.pack(side=LEFT, fill="both", expand=True)
- canvas_debug = Canvas(root, width=ww, height=hh, bg='white')
- canvas_debug.pack(side=LEFT, fill="both", expand=True)
- zoom_factor = 1.5
- xRange = 1.0
- yRange = 1.0
- xOffset = 0
- yOffset = 0
- cx = ww // 2
- cy = hh // 2
- dx = dy = 0
- precision = 400
- def rgb2hex(rgb):
- r, g, b = rgb
- return "#{:02x}{:02x}{:02x}".format(r, g, b)
- rainbow = []
- def z(r, g, b):
- rainbow.append((r, g, b))
- r, g, b = 255, 0, 0
- for g in range(256):
- z(r, g, b)
- for r in range(254, -1, -1):
- z(r, g, b)
- for b in range(256):
- z(r, g, b)
- for g in range(254, -1, -1):
- z(r, g, b)
- for r in range(256):
- z(r, g, b)
- for b in range(254, -1, -1):
- z(r, g, b)
- Lr = len(rainbow) - 1
- img = Image.new('RGB', (ww, hh))
- pixels = img.load()
- @lru_cache(maxsize=None)
- def cache_mandelbrot(x0, y0):
- x = 0.0
- y = 0.0
- k = 4000
- for i in range(precision + 1):
- x_new = x * x - y * y + x0
- y = 2 * x * y + y0
- x = x_new
- if x * x + y * y > 10:
- k = 12
- break
- j = (x * x + y * y) ** 0.5
- j = int(i * (i * 10) + j * k) % Lr
- return rainbow[j]
- def calculate_mandelbrot(xRange, yRange):
- canvas_debug.create_text(10, 20, anchor=NW, text="Processing, Please Wait...")
- canvas_debug.update()
- canvas_main.delete("all")
- for row in range(hh):
- for col in range(ww):
- x0 = ((row - 50 - cx + dx + xOffset * xRange) * 3 / ww) / xRange
- y0 = ((col - cy + dy + yOffset * yRange) * 2.5 / hh) / yRange
- pixels[col, row] = cache_mandelbrot(x0, y0)
- canvas_main.image = ImageTk.PhotoImage(img)
- canvas_main.create_image(0, 0, anchor=NW, image=canvas_main.image)
- update_debug_canvas()
- def update_debug_canvas():
- canvas_debug.delete("all")
- canvas_debug.create_text(10, 50, anchor=NW, text=f"x offset: {xOffset*-1}")
- canvas_debug.create_text(10, 70, anchor=NW, text=f"y offset: {yOffset*-1}")
- canvas_debug.create_text(10, 90, anchor=NW, text=f"zoom_factor: {xRange}")
- def on_mouse_down(event):
- global drag_data
- drag_data = {'x': event.x, 'y': event.y}
- def on_mouse_move(event):
- global dx, dy
- dx = (event.x - drag_data['x'])
- dy = (event.y - drag_data['y'])
- canvas_main.coords("all", dx, dy)
- update_debug_canvas()
- def on_mouse_up(event):
- global xOffset, yOffset, dx, dy
- dx /= yRange
- dy /= xRange
- xOffset -= dy
- yOffset -= dx
- dx = dy = 0
- calculate_mandelbrot(xRange, yRange)
- def on_key_up(event):
- global xRange, yRange
- xRange, yRange = (xRange * zoom_factor, yRange * zoom_factor)
- calculate_mandelbrot(xRange, yRange)
- def on_key_down(event):
- global xRange, yRange
- xRange, yRange = (xRange / zoom_factor, yRange / zoom_factor)
- calculate_mandelbrot(xRange, yRange)
- canvas_main.bind('<Button-1>', on_mouse_down)
- canvas_main.bind('<B1-Motion>', on_mouse_move)
- canvas_main.bind('<ButtonRelease-1>', on_mouse_up)
- root.bind('<Down>', on_key_down)
- root.bind('<Up>', on_key_up)
- calculate_mandelbrot(xRange, yRange)
- root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement