python – Mandelbrot设置显示不正确

前端之家收集整理的这篇文章主要介绍了python – Mandelbrot设置显示不正确前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
这是我尝试使用Pygame模块对 Python 3.5中的Mandelbrot集进行编程.
  1. import math,pygame
  2. pygame.init()
  3.  
  4. def mapMandelbrot(c,r,dim,xRange,yRange):
  5. x = (dim-c)/dim
  6. y = (dim-r)/dim
  7. #print([x,y])
  8. x = x*(xRange[1]-xRange[0])
  9. y = y*(yRange[1]-yRange[0])
  10. x = xRange[0] + x
  11. y = yRange[0] + y
  12. return [x,y]
  13.  
  14. def checkDrawBox(surface):
  15. for i in pygame.event.get():
  16. if i.type == pygame.QUIT:
  17. pygame.quit()
  18. elif i.type == pygame.MOUSEBUTTONDOWN:
  19. startXY = pygame.mouse.get_pos()
  20. BoxExit = False
  21. while BoxExit == False:
  22. for event in pygame.event.get():
  23. if event.type == pygame.MOUSEBUTTONUP:
  24. BoxExit = True
  25. if BoxExit == True:
  26. return [startXY,pygame.mouse.get_pos()]
  27. pygame.draw.rect(surface,[255,0],[startXY,[pygame.mouse.get_pos()[0]-startXY[0],pygame.mouse.get_pos()[1]-startXY[1]]],1)
  28. pygame.display.update()
  29.  
  30. def setup():
  31. dimensions = 500
  32. white = [255,255,255]
  33. black = [0,0]
  34. checkIterations = 100
  35. canvas = pygame.display.set_mode([dimensions,dimensions])
  36. canvas.fill(black)
  37. xRange = [-2,2]
  38. yRange = [-2,2]
  39. xRangePrev = [0,0]
  40. yRangePrev = [0,0]
  41. newxRange = [0,0]
  42. newyRange = [0,0]
  43. while True:
  44. if not ([xRange,yRange] == [xRangePrev,yRangePrev]):
  45. draw(dimensions,canvas,yRange,checkIterations)
  46. pygame.display.update()
  47. xRangePrev = xRange
  48. yRangePrev = yRange
  49. Box = checkDrawBox(canvas)
  50. if Box != None:
  51. maxX = max([Box[0][0],Box[1][0]])
  52. maxY = max([Box[0][1],Box[1][1]])
  53. newxRange[0] = mapMandelbrot(Box[0][0],dimensions,yRange)[0]
  54. newxRange[1] = mapMandelbrot(Box[1][0],yRange)[0]
  55. newyRange[0] = mapMandelbrot(0,Box[0][1],yRange)[1]
  56. newyRange[1] = mapMandelbrot(0,Box[1][1],yRange)[1]
  57. xRange = newxRange
  58. yRange = newyRange
  59.  
  60. def draw(dim,surface,checkIterations):
  61. for column in range(dim):
  62. for row in range(dim):
  63. greyVal = iteration(0,mapMandelbrot(column,row,yRange),checkIterations,checkIterations)
  64. surface.set_at([dim-column,row],greyVal)
  65.  
  66. def iteration(a,b,c,iterCount,maxIter):
  67. a = (a*a) - (b*b) + c[0]
  68. b = (2*a*b) + c[1]
  69. iterCount = iterCount - 1
  70. if iterCount == 0:
  71. return [0,0]
  72. elif abs(a+b) > 17:
  73. b = (iterCount/maxIter)*255
  74. return [b,b]
  75. else:
  76. return iteration(a,maxIter)
  77.  
  78.  
  79. setup()

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

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

解决方法

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

猜你在找的Python相关文章