2012-02-18 44 views
1

我需要弄清楚如何控制tu.py.py中的self._newline()。在我的Python Mandelbrot套裝程序中,當我開始做奇怪的事情時,我發現了這件事;有關更多詳細信息,請參閱Why is turtle lightening pixels?。但是,當我試圖製作一個極其類似的程序來繪製複數的切線時,同樣的事情也沒有發生......但是隨着時間的推移,程序顯着減速。如何控制龜的self._newline()?

基本上,我問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") 

回答

0

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

使用penup/pendown分別停止/啓動self.__newline

參考

+0

我試過這種方法與' benoit()'上面的例子,它可以有效地消除這個問題。但是,我發現如果你不小心,它也會產生額外的行文物和/或嚴重影響你的代碼的性能。我很好奇,看到你使用這種技術修復了有問題的代碼。 – cdlane 2017-07-20 03:30:32

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