加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

python – Mandelbrot设置显示不正确

发布时间:2020-12-16 23:34:36 所属栏目:Python 来源:网络整理
导读:这是我尝试使用Pygame模块对 Python 3.5中的Mandelbrot集进行编程. import math,pygamepygame.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
这是我尝试使用Pygame模块对 Python 3.5中的Mandelbrot集进行编程.
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],[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]
    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,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],dimensions,yRange)[0]
            newxRange[1] = mapMandelbrot(box[1][0],yRange)[0]
            newyRange[0] = mapMandelbrot(0,box[0][1],yRange)[1]
            newyRange[1] = mapMandelbrot(0,box[1][1],yRange)[1]
            xRange = newxRange
            yRange = newyRange

def draw(dim,surface,checkIterations):
    for column in range(dim):
        for row in range(dim):
            greyVal = iteration(0,mapMandelbrot(column,row,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]
    elif abs(a+b) > 17:
        b = (iterCount/maxIter)*255
        return [b,b]
    else:
        return iteration(a,maxIter)


setup()

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

想知道可能是什么问题?很抱歉代码转储,只是不确定哪个部分可能会导致它看起来像那样.

解决方法

迷人的bug – 它看起来像一个被压扁的bug

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读