|
| 1 | +# Julia Fractal using only math and tkinter libraries |
| 2 | +from tkinter import * |
| 3 | +from math import * |
| 4 | +# parameters |
| 5 | +step = 1 # defines resolutuon vs speed (1: max resolution, slowest) |
| 6 | +screen_width = 1600; screen_height = 1000 # image size in pixels |
| 7 | +# make tkinter and canvas objects |
| 8 | +root = Tk() |
| 9 | +root.title("Julia Fractal using Python and Tkinter") |
| 10 | +canvas1 = Canvas(root, background = "black", |
| 11 | + height = screen_height, width = screen_width) |
| 12 | +canvas1.pack() |
| 13 | +# calculation and plotting |
| 14 | +c = complex(-0.5125, 0.5213) # complex constant for julia fractal |
| 15 | +for x in range(0, screen_width, step): |
| 16 | + re = x / (screen_width - 1) * 3.0 - 1.5 |
| 17 | + #re = x / (screen_width - 1) * 1.5 - 0.75 # zoomed in version |
| 18 | + for y in range(0, screen_height, step): |
| 19 | + im = y / (screen_height - 1) * 2.0 - 1.0 |
| 20 | + #im = y / (screen_height - 1) * 1.0 - 0.5 # zoomed in version |
| 21 | + z = complex(re, im) # initial value for z |
| 22 | + i = 0 # counter will be measure for how fast z grows |
| 23 | + while abs(z) < 2.0 and i < 1024: # exit loop if |z| > 2.0 or 1024 iterations completed |
| 24 | + z = z**2 + c |
| 25 | + i += 1 |
| 26 | + i = int(sqrt(i)*8) # apply non linear scaling on i |
| 27 | + r = i % 33 * 8; r = min(255, r) # calculate color comp. from i |
| 28 | + g = i % 129 * 2; g = min(255, g) |
| 29 | + b = i % 65 * 4; b = min(255, b) |
| 30 | + col_str = f"#{r:02X}{g:02X}{b:02X}" # color in "#rrggbb" format, hex values |
| 31 | + canvas1.create_rectangle(x, y, x + step, y + step, |
| 32 | + fill = col_str, outline = col_str) # plot ontkinter canvas |
| 33 | + if x % 50 == 0: |
| 34 | + canvas1.update() # each 50 cycles show updated canvas |
| 35 | +# plotting finished, window shows |
| 36 | +mainloop() |
0 commit comments