Python 3.2和Python 2.7代码问题

4
我有一份使用Pygame绘制曼德勃罗集的代码。以下是该代码。
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
@Todd 你一定对现代艺术很感兴趣。 - ThisIsAQuestion
2个回答

10

是的,请在您的文件顶部添加以下内容:

from __future__ import division

Python 2默认使用整数输入时的整除(向下取整);Python 3即使使用整数输入也切换到浮点数除法。

请参阅文档中记录此更改的PEP 238:

对于数字参数,当前的除法(/)运算符具有模糊的含义:如果参数是int或long,则返回除法的数学结果的下限;但如果参数是floats或complex,则返回除法结果的合理近似值。当不期望但可能存在整数输入时,这使得期望浮点或复数结果的表达式容易出错。


这个很完美,但我有另一个问题。即使我在3.2上,一旦我放大了很多,我得到的图片与前面的2.7类似,我认为这是由于Python中精度不足所致。我尝试使用十进制模块来解决它,但那样做需要更长的时间。有什么解决办法吗? - ThisIsAQuestion
4
并不完全准确;浮点数确实有精度限制,但由于它们是由您的PC硬件处理的,所以处理速度也要快得多。您可以转而使用整数来计算曼德博集合。无论如何,这就是我20多年前玩分形图时使用FRACTINT的方式。 :-) - Martijn Pieters

6
在顶部添加from __future__ import division

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接