为什么海龟的像素会变亮?
我的用于创建 Mandelbrot 集的程序有一个错误:每当笔改变颜色时,以及之后的每 42 个像素,颜色都会变浅。相当巧合的是,这是一个曼德尔错误(是的,我刚刚学会了这个术语),因为它对于“边缘”附近的许多像素来说是不一致的(它实际上可能在它应该是的颜色和最后一个颜色之间模糊,或者下一个像素应该是),但它始终是该像素之后的第 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 已建议修复,他喜欢这个错误。然而,我没有编辑Python源代码的经验,所有的下划线都让我紧张。有人可以具体告诉我要编辑什么和/或如何编辑吗?
My program for creating a Mandelbrot set has a bug: whenever the pen changes colors, and every 42nd pixel after that, is lighter. This is, rather coincidentally, a mandelbug (yes, I just learned that term), as it is inconsistent for many pixels near an "edge" (it might actually be blurred between the color it's supposed to be and the color the last, or next, pixel is supposed to be), but it's always the 42nd pixel after that one until the next color change. I am using OSX 10.6.8, PYTHON 2.7. When I wrote this program at school, it worked perfectly (Windows), and then I sent it to myself, and worked on it a little more (mostly just making the sample size and therefore image larger), and ran it, I got this bug. EDIT: My bad, I forgot to mention that this only happens with my Mandelbrot program, the few other turtle programs I have at home are fine.
Parts of screenshots (so that you don't have to wait forever while the program runs to see what I'm talking about):
From my first version from home:
From the current version (sideways):
Heres the code:
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")
EDIT: A fix has been suggested by DSM, who likes this bug. However, I have no experience editing Python source code, and all the underscores are making me nervous. Can someone tell me specifically what to edit and/or how?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
哇。我认为这是我最喜欢的错误之一,不管你信不信,这个数字恰好是 42 这个事实实际上是相关的!好吧,无论如何,外围..
在turtle.py中:
所以当它决定断行时,问题就出现了,显然是出于性能原因:
我能够通过提高行数限制和/或在没有的地方分散 self._pencolor 引用来“修复”该错误没有。但无论如何,你并没有疯,而且你所做的也不是真正的事情。 :-)
Wow. I think this is one of my favourite bugs ever, and believe it or not, the fact that the number happens to be 42 is actually relevant! Well, peripherally, anyhow..
In turtle.py:
So the problem comes about when it decides to break a line, apparently for performance reasons:
I was able to "fix" the bug by bumping up the linenumber limit and/or scattering self._pencolor references in places that didn't have any. But you're not crazy, anyway, and it's not really anything that you're doing. :-)
我可以提供建议吗?
我尝试了你的代码,但你知道它需要永远运行,但你可能不知道的是跟踪器函数......我只是把它放在你的代码的开头:
这也消除了对速度的需要( 0) 函数:)
尝试一下并再次运行它,我做到了,它在 62 秒内渲染了整个图像,我通过将此代码放在开头导入时间模块来计时:
并将此代码放在末尾:
做得好顺便说一下,我刚刚做了我自己的,那就是比你的慢很多,质量也低,但使用的是方形数组,并冲压到数组中我想要的每个位置,哈哈,但将来会尝试改进它,因为我不到一周前才发现乌龟存在。
最后一件事,如果您输入:
而不是“导入海龟”,您不需要将海龟放在每个函数调用的开头:)对于每个其他模块也是如此。
我已经包含了你的分形图片,它在我的机器上渲染了 62 秒,这甚至没有那么强大你的代码运行在我的机器很弱。
我希望这一切对你有很大帮助。您还会注意到我没有那个光线问题,不确定您是否在顶部的原始代码中解决了该问题?
Can i offer a suggestion?
i tried your code and it was taking forever to run which you are aware of but what you may not be aware of is the tracer function... i simply put at the beginning of your code:
that also eliminates the need for the speed(0) function :)
Try that and run it again, i did and it rendered the whole image in 62 seconds, i timed it by importing the time module by putting this code at the beginning:
and this code at the end:
Well done by the way, Ive just made my own thats a lot slower and lower quality then yours but was using an array of the square shape and stamping to each location i wanted in the array lol, but will be trying to improve it in the future as i only found out turtle existed less then a week ago.
One last thing, if you type:
instead of "import turtle" you dont need to put turtle at the beginning of every function call :) same thing goes for every other module.
Ive included the pic of your fractal that took 62 seconds to render on my machine thats not even that powerfulYour code run on my weak machine.
I hope all this helps you greatly. also youll notice i dont have that light line problem, not sure if you fixed that issue in the original code up top?