Mandelbrot集显示不正确 [英] Mandelbrot set displays incorrectly

查看:100
本文介绍了Mandelbrot集显示不正确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我尝试使用Pygame模块对Python 3.5中的Mandelbrot集进行编程.

This is my attempt to program the Mandelbrot set in Python 3.5 using the Pygame module.

import math, pygame
pygame.init()

def mapMandelbrot(c,r,dim,xRange,yRange):
    x = (dim-c)/dim
    y = (dim-r)/dim
    #print([x,y])
    x = x*(xRange[1]-xRange[0])
    y = y*(yRange[1]-yRange[0])
    x = xRange[0] + x
    y = yRange[0] + y
    return [x,y]

def checkDrawBox(surface):
    for i in pygame.event.get():
        if i.type == pygame.QUIT:
            pygame.quit()
        elif i.type == pygame.MOUSEBUTTONDOWN:
            startXY = pygame.mouse.get_pos()
            boxExit = False
            while boxExit == False:
                for event in pygame.event.get():
                    if event.type == pygame.MOUSEBUTTONUP:
                        boxExit = True
                if boxExit == True:
                    return [startXY,pygame.mouse.get_pos()]
                pygame.draw.rect(surface,[255,0,0],[startXY,[pygame.mouse.get_pos()[0]-startXY[0],pygame.mouse.get_pos()[1]-startXY[1]]],1)
                pygame.display.update()

def setup():
    dimensions = 500
    white = [255,255,255]
    black = [0,0,0]
    checkIterations = 100
    canvas = pygame.display.set_mode([dimensions,dimensions])
    canvas.fill(black)
    xRange = [-2,2]
    yRange = [-2,2]
    xRangePrev = [0,0]
    yRangePrev = [0,0]
    newxRange = [0,0]
    newyRange = [0,0]
    while True:
        if not ([xRange,yRange] == [xRangePrev,yRangePrev]):
            draw(dimensions, canvas, xRange, yRange, checkIterations)
            pygame.display.update()
            xRangePrev = xRange
            yRangePrev = yRange
        box = checkDrawBox(canvas)
        if box != None:
            maxX = max([box[0][0],box[1][0]])
            maxY = max([box[0][1],box[1][1]])
            newxRange[0] = mapMandelbrot(box[0][0],0,dimensions,xRange,yRange)[0]
            newxRange[1] = mapMandelbrot(box[1][0],0,dimensions,xRange,yRange)[0]
            newyRange[0] = mapMandelbrot(0,box[0][1],dimensions,xRange,yRange)[1]
            newyRange[1] = mapMandelbrot(0,box[1][1],dimensions,xRange,yRange)[1]
            xRange = newxRange
            yRange = newyRange

def draw(dim, surface, xRange, yRange, checkIterations):
    for column in range(dim):
        for row in range(dim):
            greyVal = iteration(0,0,mapMandelbrot(column,row,dim,xRange,yRange),checkIterations,checkIterations)    
            surface.set_at([dim-column,row],greyVal)

def iteration(a, b, c, iterCount, maxIter):
    a = (a*a) - (b*b) + c[0]
    b = (2*a*b) + c[1]
    iterCount = iterCount - 1
    if iterCount == 0:
        return [0,0,0]
    elif abs(a+b) > 17:
        b = (iterCount/maxIter)*255
        return [b,b,b]
    else:
        return iteration(a,b,c,iterCount,maxIter)


setup()

我相信迭代算法是正确的,但是输出看起来不正确:

I believe that the iteration algorithm is correct, but the output doesn't look right:

想知道可能是什么问题?对不起,代码转储,只是不确定哪一部分可能导致它看起来像这样.

Wondering what might be the problem? Sorry for the code dump, just not sure which part may cause it to look like that.

推荐答案

引人入胜的bug -从字面上看,它像是一个压缩过的bug:)

Fascinating bug -- it literally looks like a squashed bug :)

问题出在两行:

a = (a*a) - (b*b) + c[0]
b = (2*a*b) + c[1]

您在第一行中更改了a的含义,因此在第二行中使用了错误的a.

You are changing the meaning of a in the first line, hence using the wrong a in the second.

解决方法很简单:

a, b  = (a*a) - (b*b) + c[0], (2*a*b) + c[1]

,这将导致在计算右侧时使用相同的a值.

which will cause the same value of a to be used in calculating the right hand side.

弄清楚您的bug产生了什么会很有趣.即使它不是Mandelbrot集合,它本身也似乎是一个有趣的分形.从这个意义上讲,您有一个非常幸运的错误. 99%的时间,错误会导致垃圾,但是有时它们会产生一些非常有趣的东西,但是却是意想不到的.

It would be interesting to work out just what your bug has produced. Even though it isn't the Mandelbrot set, it seems to be an interesting fractal in its own right. In that sense, you had a very lucky bug. 99% percent of the times, bugs lead to garbage, but every now and then they produce something quite interesting, but simply unintended.

编辑时:

Mandelbrot集基于迭代复杂的多项式:

The Mandelbrot set is based on iterating the complex polynomial:

f(z) = z^2 + c

此错误产生的伪Mandelbrot集基于迭代函数

The pseudo-Mandelbrot set which this bug has produced is based on iterating the function

f(z) = Re(z^2 + c) + i*[2*Re(z^2 + c)*Im(z) + Im(c)]

其中,Re()Im()是提取复数的实部和虚部的运算符.这不是z中的多项式,尽管很容易看出它是z,z*中的多项式(其中z*z的复共轭).由于它是一个很自然的错误,因此几乎可以肯定它已经出现在Mandelbrot集的文献中,尽管我不记得曾经见过.

where Re() and Im() are the operators which extract the real and imaginary parts of a complex number. This isn't a polynomial in z, though it is easy to see that it is a polynomial in z,z* (where z* is the complex conjugate of z). Since it is a fairly natural bug, it is almost certain that this has appeared somewhere in the literature on the Mandelbrot set, though I don't remember ever seeing it.

这篇关于Mandelbrot集显示不正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆