The most pressing problem i have with my code is that when i change the X and Y dimensions to for example X = 501, Y = 500 the mandelbrot set tears completely (see pictures). The X and Y axis are also inverted.
My goal is to achieve a similar result as this http://code.activestate.com/recipes/579048-python-mandelbrot-fractal-with-tkinter/ and from what i can gather i should create a coordinate mapping centralised around the origin?
Any help would be greatly appreciated.
from tkinter import *
import numpy as np
from numba import jit
X = 500
Y = 500
maxIter = 500
minR = -3
minI = -2
maxR = 2
maxI = 2
@jit
def mandelbrot_set(minR,maxR,minI,maxI,X,Y,maxIter):
r1 = np.linspace(minR, maxR, X)
r2 = np.linspace(minI, maxI, Y)
return (r1,r2,[mandelbrot(complex(r, i),maxIter) for r in r1 for i in r2])
@jit
def mandelbrot(c,max):
z = c
for n in range(max):
if abs(z) > 4:
return n
z = z*z + c
return 255
set = mandelbrot_set(minR,maxR,minI,maxI,X,Y,maxIter)
window = Tk()
canvas = Canvas(window, width = X, height = Y, bg = "#FFFFFF", highlightthickness=0)
canvas.pack()
img = PhotoImage(width = X, height = Y)
canvas.create_image((0, 0), image = img, state = "normal", anchor = NW)
hexstring = ""
counter = 0
for imaginary in set[1]:
hexstring += "{ "
for real in set[0]:
if set[2][counter] == 0:
hexstring += "#000000 "
else:
hexstring += "#" + "%02x" % set[2][counter] + "%02x" % set[2][counter] + "%02x" % set[2][counter] + " "
counter += 1
hexstring += "} "
img.put(hexstring)
window.mainloop()
Normal:
Broken mandelbrot:
To resolves problem with tearing you have to replace set[0]
with set[1]
in code
for imaginary in set[0]: # before set[1]
hexstring += "{ "
for real in set[1]: # before set[0]
Code:
from tkinter import *
import numpy as np
from numba import jit
X = 510
Y = 500
maxIter = 500
minR = -3
minI = -2
maxR = 2
maxI = 2
@jit
def mandelbrot_set(minR, maxR, minI, maxI, X, Y, maxIter):
r1 = np.linspace(minR, maxR, X)
r2 = np.linspace(minI, maxI, Y)
return (r1, r2, [mandelbrot(complex(r, i), maxIter) for r in r1 for i in r2])
@jit
def mandelbrot(c,max):
z = c
for n in range(max):
if abs(z) > 4:
return n
z = z*z + c
return 255
set = mandelbrot_set(minR,maxR,minI,maxI,X,Y,maxIter)
window = Tk()
canvas = Canvas(window, width=X, height=Y, bg="#FFFFFF", highlightthickness=0)
canvas.pack()
img = PhotoImage(width=X, height=Y)
canvas.create_image((0, 0), image = img, state = "normal", anchor = NW)
hexstring = ""
counter = 0
for imaginary in set[0]:
hexstring += "{ "
for real in set[1]:
if set[2][counter] == 0:
hexstring += "#000000 "
else:
hexstring += "#" + "%02x" % set[2][counter] + "%02x" % set[2][counter] + "%02x" % set[2][counter] + " "
counter += 1
hexstring += "} "
img.put(hexstring)
window.mainloop()
To rotated image you have to replace for r in r2
with for i in r1
in
[mandelbrot(complex(r, i), maxIter) for i in r2 for r in r1]
but keep previous
for imaginary in set[1]: # before set[1]
hexstring += "{ "
for real in set[0]: # before set[0]
Code:
from tkinter import *
import numpy as np
from numba import jit
X = 510
Y = 500
maxIter = 500
minR = -3
minI = -2
maxR = 2
maxI = 2
@jit
def mandelbrot_set(minR, maxR, minI, maxI, X, Y, maxIter):
r1 = np.linspace(minR, maxR, X)
r2 = np.linspace(minI, maxI, Y)
return (r1, r2, [mandelbrot(complex(r, i), maxIter) for i in r2 for r in r1])
@jit
def mandelbrot(c,max):
z = c
for n in range(max):
if abs(z) > 4:
return n
z = z*z + c
return 255
set = mandelbrot_set(minR,maxR,minI,maxI,X,Y,maxIter)
window = Tk()
canvas = Canvas(window, width=X, height=Y, bg="#FFFFFF", highlightthickness=0)
canvas.pack()
img = PhotoImage(width=X, height=Y)
canvas.create_image((0, 0), image = img, state = "normal", anchor = NW)
hexstring = ""
counter = 0
for imaginary in set[1]:
hexstring += "{ "
for real in set[0]:
if set[2][counter] == 0:
hexstring += "#000000 "
else:
hexstring += "#" + "%02x" % set[2][counter] + "%02x" % set[2][counter] + "%02x" % set[2][counter] + " "
counter += 1
hexstring += "} "
img.put(hexstring)
window.mainloop()