2013-04-18 80 views
4

我有一個代碼來繪製使用pygame的mandlebrot集。這是代碼。Python 3.2與Python 2.7代碼問題

import pygame, sys, math 
from decimal import * 
window=pygame.display.set_mode((1000, 1000)) 
window.fill((255, 255, 255)) 
pygame.display.update() 
winrect=window.get_rect() 
hq=3 
getcontext().prec=20 
colors=((255, 0, 0), (255, 128, 0), (255, 255, 0), (128, 255, 0), (0, 255, 0), (0, 255, 128), (0, 255, 255), (0, 128, 255), (0, 0, 255), (128, 0, 255), (255, 0, 255), (255, 0, 128)) 
def graph(scale):#left, right, bottom, top 
    window.fill((0, 0, 0)) 
    minimum=-1 
    y=((scale[3]-scale[2]))/(winrect.height)+scale[2] 
    for a in range(winrect.width): 
     x=((scale[1]-scale[0])*(a))/(winrect.width)+scale[0] 
     d, e=x**2-y**2+x, 2*x*y+y 
     for i in range(int(1/(50*(scale[1]-scale[0]))+25)): 
      d, e=d**2-e**2+x, 2*d*e+y 
      if math.sqrt(d**2+e**2)>2: 
       if i<minimum or minimum==-1: 
        minimum=i 
       break 
    y=((scale[3]-scale[2])*winrect.height)/(winrect.height)+scale[2] 
    for a in range(winrect.width): 
     x=((scale[1]-scale[0])*a)/winrect.width+scale[0] 
     d, e=x**2-y**2+x, 2*x*y+y 
     for i in range(int(1/(50*(scale[1]-scale[0]))+25)): 
      d, e=d**2-e**2+x, 2*d*e+y 
      if math.sqrt(d**2+e**2)>2: 
       if i<minimum or minimum==-1: 
        minimum=i 
       break 
    x=((scale[1]-scale[0])*1)/winrect.width+scale[0] 
    for b in range(winrect.height): 
     y=((scale[3]-scale[2])*b)/winrect.height+scale[2] 
     d, e=x**2-y**2+x, 2*x*y+y 
     for i in range(int(1/(50*(scale[1]-scale[0]))+25)): 
      d, e=d**2-e**2+x, 2*d*e+y 
      if math.sqrt(d**2+e**2)>2: 
       if i<minimum or minimum==-1: 
        minimum=i 
       break 
    x=((scale[1]-scale[0])*winrect.width)/winrect.width+scale[0] 
    for b in range(winrect.height): 
     y=((scale[3]-scale[2])*b)/winrect.height+scale[2] 
     d, e=x**2-y**2+x, 2*x*y+y 
     for i in range(int(1/(50*(scale[1]-scale[0]))+25)): 
      d, e=d**2-e**2+x, 2*d*e+y 
      if math.sqrt(d**2+e**2)>2: 
       if i<minimum or minimum==-1: 
        minimum=i 
       break 
    for a in range(winrect.width): 
     for b in range(winrect.height): 
      x=((scale[1]-scale[0])*a)/winrect.width+scale[0] 
      y=((scale[3]-scale[2])*b)/winrect.height+scale[2] 
      d, e=x**2-y**2+x, 2*x*y+y 
      for i in range(minimum): 
       d, e=d**2-e**2+x, 2*d*e+y 
      for i in range(20*hq): 
       d, e=d**2-e**2+x, 2*d*e+y 
       if math.sqrt(d**2+e**2)>2: 
        window.set_at((a, b), colors[i-(int(i/len(colors))*len(colors))]) 
        break 
      for event in pygame.event.get(): 
       if event.type==pygame.QUIT: 
        pygame.quit() 
        sys.exit() 
       if event.type==pygame.KEYDOWN: 
        if event.key==pygame.K_ESCAPE: 
         pygame.quit() 
         sys.exit() 
     pygame.display.update() 
    pygame.display.update() 
graph([-3, 2, -2.5, 2.5, 0])# 
scale=[-3, 2, -2.5, 2.5, 0] 
scalea=scale[:] 
while True: 
    for event in pygame.event.get(): 
     if event.type==pygame.QUIT: 
      pygame.quit() 
      sys.exit() 
     if event.type==pygame.KEYDOWN: 
      if event.key==pygame.K_ESCAPE: 
       pygame.quit() 
       sys.exit() 
      if event.key==pygame.K_r: 
       graph([-3, 2, -2.5, 2.5, 0]) 
       scale=[-3, 2, -2.5, 2.5, 0] 
       scalea=scale[:] 
      if event.key==pygame.K_p: 
       hq+=1 
       graph(scale) 
      if event.key==pygame.K_o: 
       if not hq==1: 
        hq-=1 
        graph(scale) 
      if event.key==pygame.K_SPACE: 
       print(scale) 
       print(scale[1]-scale[0]) 
     if event.type==pygame.MOUSEBUTTONDOWN: 
      if not scalea[4]: 
       scalea[0]=(((scale[1]-scale[0])*event.pos[0])/winrect.width)+scale[0] 
       scalea[2]=(((scale[3]-scale[2])*event.pos[1])/winrect.height)+scale[2] 
       scalea[4]=1 
      else: 
       scalea[1]=(((scale[1]-scale[0])*event.pos[0])/winrect.width)+scale[0] 
       scalea[3]=(((scale[3]-scale[2])*event.pos[1])/winrect.height)+scale[2] 
       scalea[4]=0 
       if scalea[1]<scalea[0]: 
        scalea=[scalea[1], scalea[0], scalea[2], scalea[3], 0] 
       if scalea[3]<scalea[2]: 
        scalea=[scalea[0], scalea[1], scalea[3], scalea[2], 0] 
       scale=scalea[:] 
       if scale[1]-scale[0]<scale[3]-scale[2]: 
        scale[1]+=((scalea[3]-scalea[2])-(scalea[1]-scalea[0]))/2 
        scale[0]-=((scalea[3]-scalea[2])-(scalea[1]-scalea[0]))/2 
       else: 
        scale[2]-=((scalea[1]-scalea[0])-(scalea[3]-scalea[2]))/2 
        scale[3]+=((scalea[1]-scalea[0])-(scalea[3]-scalea[2]))/2 
       graph(scale) 

當我在python 3.2中運行它時,它工作正常。圖像看起來是這樣的:

[IMG]http://i47.tinypic.com/2ps0d8n.jpg[/IMG]

然而,當我在Python 2.7中運行它,我得到了一個可怕看圖像,看起來像這樣:

[IMG]http://i47.tinypic.com/2a0nskk.jpg[/IMG]

有什麼辦法我可以修復這個?

+7

你只會說「可怕」,因爲它不是你所期望的。我認爲它看起來很大! – Todd

+1

看起來像一個「BIT.TRIP Mandelbrot」... ^^ – poke

+0

@Todd你必須進入現代藝術。 – PygameNerd

回答

10

是的,在你的文件的頂部添加此:

from __future__ import division 

的Python 2使用整數除法(樓科)默認情況下使用整數輸入時;即使使用整數輸入,Python 3也會切換到浮點除法。

PEP 238,其中記錄了變化:

當前劃分(/)操作員具有用於 數值參數不明確的含義:它返回除法的數學 結果如果參數的地板整數或長整數,但如果 參數浮點或複雜,則它返回除法結果的合理近似值。這使得表達式期望 漂浮或複雜結果容易出錯,當整數不是 預期但可能作爲輸入。

+0

這工作完美,但我有另一個問題。即使在3.2版本中,當我放大很多時,我也會看到與前者2.7類似的圖片,我認爲這是由於Python的精度不夠。我使用了十進制模塊來試圖擺脫它,但是當我這樣做時,它會花費更長的時間。任何解決方案? – PygameNerd

+4

不是真的;浮點確實具有精確限制,但是由於它們是由您的PC硬件處理的,所以它們的處理速度也更快。您可以改用[整數來計算mandelbrot](http://www.nahee.com/spanky/www/fractint/)。無論如何,這就是20年前我用FRACTINT玩弄分形的原因。 :-) –

6

在頂部添加from __future__ import division