我用于创建 Mandelbrot 集的程序有一个错误:每当笔改变颜色时,并且之后每隔 42 个像素变亮。巧合的是,这是一个 mandelbug(是的,我刚刚学到这个术语),因为它对于“边缘”附近的许多像素不一致(它实际上可能在它应该是的颜色和最后一个颜色之间模糊,或者接下来,像素应该是),但它始终是该像素之后的第 42 个像素,直到下一个颜色发生变化。我正在使用 OSX 10.6.8、PYTHON 2.7。当我在学校写这个程序时,它运行得很好(Windows),然后我把它发给自己,再做一些工作(主要是使样本大小,因此图像更大),然后运行它,我得到了这个漏洞。编辑:糟糕,我忘了说这只发生在我的 Mandelbrot 程序中,我家里的其他几个 turtle 程序都很好。
部分屏幕截图(这样您就不必在程序运行时永远等待以查看我在说什么):
来 self 在家的第一个版本:
从当前版本(横向):
代码如下:
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 已经建议修复。然而,我没有编辑 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 引用来“修复”该错误。但无论如何,你并不疯狂,而且你所做的也不是什么。 :-)
关于python - 为什么 turtle 会照亮像素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9242241/