我如何控制turtle的self._newline()?

时间:2012-02-18 16:24:53

标签: python api debugging turtle-graphics

我需要弄清楚如何在turtle.py中控制self._newline()。我在我的python Mandelbrot设置程序中发现了这个,当它开始做奇怪的事情时;有关详细信息,请参阅Why is turtle lightening pixels?。然而,当我试图创建一个非常相似的程序,绘制复数的切线时,同样的事情没有发生......但是程序随着时间的推移而显着减慢。

基本上,我问了3个问题:

导致这种差异的这些程序有什么区别? (智力调查)

如何激活/停止self._newline()? (必要的,主要问题)

如何防止self._newline()导致颜色偏差(DSM建议我将self._pencolor()引用插入turtle.py,但我不知道如何做到这一点)? (没必要,但需要)

即使您没有回答中间问题,您的输入仍然会受到高度赞赏!

复杂的切线代码:

import turtle
import math
import cmath
turtle.speed(0)
def bengant(size, onelen):
    turtle.left(90)
    for x in range(-size*onelen, size*onelen+1):
        turtle.up()
        turtle.goto(x, -size*onelen-1)
        turtle.down()
        for y in range(-size*onelen, size*onelen+1):
            c = complex(x*1.0/onelen,y*1.0/onelen)
            k = cmath.tan(c)
            turtle.pencolor(0,math.atan(k.real)/math.pi+1/2,math.atan(k.imag)/math.pi+1/2)
            turtle.forward(1)
bengant(2,100)
x = raw_input("Press Enter to Exit")

2 个答案:

答案 0 :(得分:0)

  

如何激活/停止self._newline()? (必要的,主要问题)

使用penup / pendown分别停止/激活self.__newline

<强>参考

答案 1 :(得分:0)

  

导致此问题的这些程序之间有什么区别   差异?

问题出现在单bengant()程序中经常发生的长单色线。如果我把它变得更单色(即将0作为第三个颜色三重而不是math.atan(k.imag) / math.pi + 1/2),它会出现:

enter image description here

检测Python的海龟库确认您在这些点上达到了优化条款。

  

如何激活/停止self._newline()?

你不是。问题不在于这种优化存在,问题是它的实现存在问题。但正如您在最新的bengant()程序中所看到的那样,当涉及更多复杂性时,它会消失。也许是一个错误的报告给正确的人以正确的例子。

  

如何防止self._newline()导致颜色偏差?

benoit()代码而言,您可以使用1.5的线宽而不是默认值1来有效地消除它。它似乎不会过多地影响图像质量:

enter image description here

左边是1.0,右边是1.5。但是,每42像素的线条将消失。另一种方法是在您的颜色值中添加一些随机噪声(小的分数加法),这些颜色值不会在视觉上对人类产生影响,但会使麻烦的优化不再触发。

这是我使用此修补程序和一些速度优化对您的benoit()代码进行的返工:

import turtle

def benoit(onelen):
    turtle.tracer(False)
    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)
            g = 0

            for k in range(20):
                z = z * z + c
                if abs(z) > 2:
                    g = 0.2 + 0.8 * (20 - k) / 20
                    break

            turtle.pencolor(0, g, 0)
            turtle.forward(1)

        turtle.update()

    turtle.tracer(True)

turtle.setup(1000, 750)
turtle.hideturtle()
turtle.setundobuffer(None)
turtle.pensize(1.5)  # work around for "42" glitch

benoit(250)

turtle.exitonclick()

我在bengant()代码中按类似的方式返工:

import math
import cmath
import turtle

def bengant(size, onelen):
    turtle.tracer(False)

    turtle.left(90)

    size_onelen = size * onelen

    for x in range(-size_onelen, size_onelen + 1):
        turtle.up()
        turtle.goto(x, -size_onelen - 1)
        turtle.down()

        for y in range(-size_onelen, size_onelen + 1):
            c = complex(x * 1.0 / onelen, y  * 1.0 / onelen)
            k = cmath.tan(c)
            turtle.pencolor(0, math.atan(k.real) / math.pi + 1/2, math.atan(k.imag) / math.pi + 1/2)
            turtle.forward(1)

        turtle.update()

    turtle.tracer(True)

turtle.hideturtle()

bengant(2, 100)

turtle.exitonclick()