我的代码面临的最紧迫的问题是,当我将X和Y尺寸更改为例如X = 501,Y = 500时,曼德罗布特装置完全撕裂(参见图片)。 X和Y轴也反转。
我的目标是获得与http://code.activestate.com/recipes/579048-python-mandelbrot-fractal-with-tkinter/类似的结果,从我可以收集的信息中,我应该创建围绕原点集中的坐标映射?
任何帮助将不胜感激。
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()
正常:
破碎的mandelbrot:
最佳答案
要解决撕裂问题,您必须在代码中用set[0]
替换set[1]
for imaginary in set[0]: # before set[1]
hexstring += "{ "
for real in set[1]: # before set[0]
码:
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()
要旋转图像,您必须将
for r in r2
替换为for i in r1
[mandelbrot(complex(r, i), maxIter) for i in r2 for r in r1]
但保持先前
for imaginary in set[1]: # before set[1]
hexstring += "{ "
for real in set[0]: # before set[0]
码:
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()
关于python - Mandelbrot将“X”和“Y”值设置为“撕裂”,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/48172983/