其他分享
首页 > 其他分享> > 为什么乌龟减轻像素?

为什么乌龟减轻像素?

作者:互联网

我创建Mandelbrot集的程序有一个错误:每当笔改变颜色时,此后的第42个像素就会变亮.碰巧的是,这是一个mandelbug(是的,我刚刚学到了该术语),因为“边缘”附近的许多像素都不一致(实际上它在应该是的颜色和最后一个颜色之间可能是模糊的,或者接下来,应该假定为像素),但在下一个颜色更改之前,它始终是该像素之后的第42个像素.我正在使用OSX 10.6.8,PYTHON 2.7.当我在学校编写该程序时,它可以完美运行(Windows),然后将其发送给自己,然后再进行一些处理(主要是使样本大小增大,因此图像更大),然后运行它,我得到了错误.编辑:我不好,我忘了提到这只发生在我的Mandelbrot程序中,我在家其他几个乌龟程序也很好.

屏幕截图的一部分(这样您就不必在程序运行时就永远等待我在说什么):

从我在家的第一个版本中:

从当前版本(横向):

这是代码:

import turtle
import math
turtle.speed(0)
def benoit(onelen):
    turtle.left(90)
    for x in range(-2*onelen, onelen):
        turtle.up()
        turtle.goto(x, int(-1.5*onelen)-1)
        turtle.down()
        for y in range(int(-1.5*onelen)-1, int(1.5*onelen)-1):
            z = complex(0,0)
            c = complex(x*1.0/onelen,y*1.0/onelen)
            for k in range(20):
                z = z*z+c
                if abs(z) > 2:
                    g = .2 + .8*(20-k)/20
                    break
                if k == 19:
                    g = 0
            turtle.pencolor(0,g,0)
            turtle.forward(1)
benoit(250)
x = raw_input("Press Enter to Exityadayadayada")

编辑:DSM已建议修复,他喜欢此bug.但是,我没有编辑Python源代码的经验,所有下划线都使我感到紧张.有人可以告诉我具体编辑什么和/或如何做吗?

解决方法:

哇.我认为这是我有史以来最喜欢的错误之一,信不信由你,这个数字恰好是42,这实际上是有意义的!好吧,外围,无论如何..
在turtle.py中:

   def _goto(self, end):
        """Move the pen to the point end, thereby drawing a line
        if pen is down. All other methodes for turtle movement depend
        on this one.

[...]

    ######    vererbung!!!!!!!!!!!!!!!!!!!!!!
    self._position = end
    if self._creatingPoly:
        self._poly.append(end)
    if len(self.currentLine) > 42: # 42! answer to the ultimate question
                                   # of life, the universe and everything
        self._newLine()
    self._update() #count=True)

所以问题出在决定断线时,显然是出于性能原因:

def _newLine(self, usePos=True):
    """Closes current line item and starts a new one.                                              
       Remark: if current line became too long, animation                                          
       performance (via _drawline) slowed down considerably.                                       
    """

我可以通过提高行数限制和/或将self._pencolor引用分散在没有任何引用的地方来“修复”该错误.但是,无论如何,您并不疯狂,您所做的也不是什么.

标签:turtle-graphics,python,mandelbrot
来源: https://codeday.me/bug/20191010/1888145.html