gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 02:34:52 25 4
gpt4 key购买 nike

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

splinter 的曼德尔布罗:

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

enter image description here


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

enter image description here

关于python - Mandelbrot 将 'tearing' 设置为不同的 X 和 Y 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48172983/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com