Mandelbrot 将 'tearing' 设置为不同的 X 和 Y 值

Mandelbrot set 'tearing' at different X and Y values

我的代码最紧迫的问题是,当我将 X 和 Y 尺寸更改为例如 X = 501、Y = 500 时,mandelbrot 完全撕裂(见图)。 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()

正常:

破碎的曼德布洛特:

要解决撕裂问题,您必须将代码

中的 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 in

 [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()