Mandelbrot以不同的X和Y值设置'撕裂'

时间:2018-01-09 16:49:57

标签: python graphics mandelbrot

我对我的代码最紧迫的问题是,当我将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()

正常:

Normal

破碎的mandelbrot:

Broken mandelbrot

1 个答案:

答案 0 :(得分:2)

要解决撕裂问题,您必须在代码中将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()

enter image description here

要旋转图片,您必须将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()

enter image description here