Python 打印 Mandelbrot 集的代码

Python code for printing Mandelbrot set

我正在尝试打印以下代码中的 Mandelbrot 集,但它打印出半屏白色和半屏黑色。我已经多次检查代码,但我不知道我哪里出错了。有人可以帮忙吗

from tkinter import *

size = 400
black = "#000000"
window = Tk()
canvas = Canvas(window, width=size, height=size, bg="white")
canvas.pack()
img = PhotoImage(width=size, height=size)
canvas.create_image((size/2, size/2), image=img, state="normal")

# constants:
MaxValue = 4
MaxIterations = 50
planeWidth = 4

for x in range(0, size):
    for y in range(0, size): # for each pixel do:
        cReal = (x * planeWidth / size) - 2
        cImg =  (y * planeWidth / size) - 2
        zReal = 0
        zImg = 0
        count = 0
        while (zReal*zReal + zImg*zImg) <= MaxValue and count < MaxIterations:
            temp = (zReal * zReal) - (zImg * zImg) + cReal
            zImg = 2 * zReal * zImg * cImg
            zReal = temp
            count += 1

        if count == MaxIterations:
            img.put(black, (x,y))

window.mainloop()

错误出现在您的更新步骤中:

            temp = (zReal * zReal) - (zImg * zImg) + cReal
            zImg = 2 * zReal * zImg * cImg
            zReal = temp

具体来说,更新zImg行末尾的* cImg应该是+ cImg.

一旦你这样做了,它就会成为一张漂亮的小 Mandlebrot 图片。