如何控制乌龟的self._newline()? [英] How do I control turtle's self._newline()?

查看:50
本文介绍了如何控制乌龟的self._newline()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要弄清楚如何在turtle.py中控制self._newline().我在python Mandelbrot set程序中就发现了这一点,当时它开始做一些奇怪的事情.有关更多详细信息,请参见为什么乌龟要减轻像素?.但是,当我尝试制作一个非常相似的程序来绘制复数的正切图时,却没有发生相同的事情……但是随着时间的流逝,该程序的运行速度大大降低了.

I need to figure out how to control the self._newline(), in turtle.py. I found out about this during my python Mandelbrot set program, when it started doing weird things; see Why is turtle lightening pixels? for more details. However, when I tried to make an extremely similar program that graphed the tangent of complex numbers, the same thing did not happen...but the program slowed down considerably over time.

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

Basically, I am asking 3 questions:

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

What is the difference between these programs that causes this discrepancy? (intellectual inquiry)

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

How do I activate/stop self._newline()? (Necessary, main question)

如何防止self._newline()引起颜色偏差(DSM建议我将self._pencolor()引用插入turtle.py,但我不知道该怎么做)? (不是必需的,但是是必需的)

How do I keep self._newline() from causing color deviations (DSM suggested that I insert self._pencolor() references into turtle.py, but I have no idea how to do this)? (Not necessary, but desired)

即使您不回答中间问题,也将非常感谢您的投入!

Even if you do not answer the middle question, your input will still be greatly appreciated!

复杂的切线代码:

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")

推荐答案

这些程序之间的区别是什么导致 差异?

What is the difference between these programs that causes this discrepancy?

问题是由于长单色线出现的,而在您的bengant()程序中,这种单色线不会经常出现.如果我将其设置为更加单色(即将0传递为第三色三元组而不是math.atan(k.imag) / math.pi + 1/2),则会显示以下外观:

The problem happens with long monochromatic lines which don't occur often enough in your bengant() program. If I make it more monochromatic (i.e. pass 0 as third color triple instead of math.atan(k.imag) / math.pi + 1/2) it makes an appearance:

插入Python的turtle库可以确认您在这些点上都点击了优化子句.

Instrumenting Python's turtle library confirms you're hitting the optimization clause at these points.

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

How do I activate/stop self._newline()?

你不知道.问题不在于此优化是否存在,问题在于它的实现存在问题.但是,正如您在最新的bengant()程序中所看到的那样,当涉及更多复杂性时,它会消失.也许会以正确的示例向正确的人报告错误.

You don't. The problem isn't that this optimization exists, the problem is there's something wrong in its implementation. But as you can see in your latest bengant() program, it disappears when more complexity is involved. Perhaps a bug report to the right people with the right example.

如何防止self._newline()引起颜色偏差?

How do I keep self._newline() from causing color deviations?

就您的benoit()代码而言,您可以使用1.5的行宽(而不是默认的1)有效地消除它.这似乎对图像质量没有太大影响:

As far as your benoit() code goes, you can effectively eliminate it using a line width of 1.5 instead of the default 1. It doesn't seem to affect the image quality too much:

在左边是1.0,在右边是1.5.但是,您每隔42个像素的行就会消失.另一种方法是在您的颜色值中添加一些随机噪声(小的分数添加),这些噪声不会对人类造成视觉上的影响,但不会触发麻烦的优化.

That's 1.0 on the left, 1.5 on the right. However, your lines every 42 pixels will disappear. Another approach would be to add some random noise (small fractional additions) to your color values that don't affect it visually for humans but keep the troublesome optimization from triggering.

这是我通过此修复程序和一些速度优化对您的benoit()代码进行的重做:

Here's my rework of your benoit() code with this fix and some speed optimizations:

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()代码的类似修改:

Here's my rework of your bengant() code along similar lines:

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()

这篇关于如何控制乌龟的self._newline()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆