2017-08-23 44 views
1

這是我第一次發佈一個問題。 我在創建涉及餘弦的代碼時遇到了問題,而且我沒有收到預期的結果。更令人困惑的是這兩個代碼應該創造類似的圖像(稍後解釋)。有任何想法嗎?兩個基於三角的海龜代碼沒有給出類似的輸出

在下面的代碼中,這些變量代表: Y是一個計數器,確保代碼只運行到產生指定數量的輻射。 W是隨機生成的顏色。 Z是從0度開始的角度轉彎。 (烏龜的角度因turtle.home而重置)。 相鄰是從中心到一條線的最小長度。 Radi是從中心突出的線條數量。

def Triangle(Radi, Adjacent): 
y = 0 
if (Radi) % 1 == 0: 
    while (Radi) > y: 
     y = y + 1 
     w = randhex() 
     z = 360/(Radi)*y 
     turtle.left(z+30) 
     turtle.color(w) 
     if z > 300: 
      turtle.forward(Adjacent/math.cos(math.pi*(60 - (z - 300))/180)) 
     elif z > 240: 
      turtle.forward(Adjacent/math.cos(math.pi*(z - 240)/180)) 
     elif z > 180: 
      turtle.forward(Adjacent/math.cos(math.pi*(60 - (z - 180))/180)) 
     elif z > 120: 
      turtle.forward(Adjacent/math.cos(math.pi*(z - 120)/180)) 
     elif z > 60: 
      turtle.forward(Adjacent/math.cos(math.pi*(60 - (z - 60))/180)) 
     else: 
      turtle.forward(Adjacent/math.cos(math.pi*z/180)) 
     turtle.home() 

以上是這似乎工作,讓這些結果被輸入Triangle(100,180)時,我的第一個代碼(請注意,randhex()是產生隨機顏色的自定義功能)。

Triangle(100,180) results.

我的道歉,如果我的變量命名的創意是煩人。 在這段代碼中,計數器表示「Y」和角度表示從以前的代碼 「Z」這裏是我的第二個代碼:

def Polygon(Radi, Adjacent, Sides): 
    counter = 0 
    if Sides % 1 != 0 or Sides == 2 or Sides <= 0: 
     print ("INVALID") 
    elif Sides == 1: 
     while Radi > counter: 
      counter = counter + 1 
      colour = randhex() 
      turn = 360/Radi*counter 
      turtle.left(turn) 
      turtle.color(colour) 
      turtle.forward(Adjacent) 
      turtle.home() 
    else: 
     while Radi > counter: 
      counter = counter + 1 
      colour = randhex() 
      turn = 360/Radi*counter 
      turtle.left(turn) 
      turtle.color(colour) 
      segment = str(counter/Radi*Sides*2) 
      position = segment.index('.') 
      test = int(segment[:position]) 
      if test % 2 == 1: 
       length = Adjacent/math.cos(math.pi*(turn - (360 - 360/Sides*((test+1)/2)))/180) 
       turtle.forward(length) 
      else: 
       length = Adjacent/math.cos(math.pi*(180/Sides - (turn - (360 - 180/Sides*(test+1))))/180) 
       turtle.forward(length) 
      turtle.home() 

以上就是我的第二個代碼,是一個我與掙扎。再次,道歉我的變量名稱是煩人的,一些數學不簡化。我發現當我離開它們時,我的想法是有意義的。以下是輸入Polygon(180,100,3)後我的第二個代碼的結果。

Polygon(180,100,3) results.

正如你所看到的,它並沒有去,我很是如何規劃。 我也應該注意到,我試着用數字代入其中一個代碼給出不同行長度的代碼。有時他們甚至走向相反的方向(因爲這個數字是負數)。我在谷歌計算器上這樣做了,但是它似乎都會給出相同的答案,但它們與第二個代碼輸出的內容相對應,而不是第一個。

如果你想我解釋任何留下評論。 但是,如果事實證明我的代碼是錯誤的(我相信),請您指出我需要做的是什麼。 我會很感激的幫助。

回答

0

您的代碼太複雜,無法調試。無用的變量名稱,缺少評論和過長的公式使得它很難閱讀。

如果我們考慮這個答案提出的公式,Is there an equation to describe regular polygons?那麼你的原三角形的代碼可以簡化爲:

import math 
import turtle 

def randhex(): 
    """ substitute random color generator here """ 
    return 'red' 

def triangle(radii, adjacent): 

    if radii % 1 != 0: # only whole numbers (int or float) allowed 
     return 

    counter = 1 

    while counter <= radii: 

     angle = counter * (2 * math.pi/radii) 
     turtle.setheading(angle) 

     colour = randhex() 
     turtle.color(colour) 

     radius = adjacent/math.cos(angle % (math.pi/1.5) - math.pi/3) 

     turtle.forward(radius) 
     turtle.backward(radius) 

     counter += 1 

turtle.radians() # avoid individual conversions, switch to radians 

triangle(100, 180) 

turtle.exitonclick() 

與一般的多邊形的解決方案可以用短短的變化來實現:

import math 
import turtle 

def randhex(): 
    """ substitute random color generator here """ 
    return 'red' 

def polygon(radii, adjacent, sides): 

    if radii % 1 != 0: # only whole numbers (int or float) allowed 
     return 

    if sides % 1 != 0 or sides == 2 or sides <= 0: 
     return 

    counter = 1 

    while counter <= radii: 

     angle = counter * (2 * math.pi/radii) 
     turtle.setheading(angle) 

     colour = randhex() 
     turtle.color(colour) 

     if sides == 1: # special case, a circle 
      radius = adjacent 
     else: 
      radius = adjacent/math.cos(angle % (math.pi/(sides/2)) - math.pi/sides) 

     turtle.forward(radius) 
     turtle.backward(radius) 

     counter += 1 

turtle.radians() # avoid individual conversions, switch to radians 

polygon(100, 180, 3) 

turtle.exitonclick() 

隨着polygon(100, 90, 5)看起來像:

enter image description here

+0

謝謝你努力解密我的代碼。我從來沒有使用'爲我在範圍'和'如果不是之前的例子。但是我仍然想知道爲什麼我的三角形代碼在我將它打入計算器時顯示出不正確的長度後仍能正常工作。 –

+0

@AlexanderJephaf,'for'循環可以更好地表達一些'while循環的情況,但是我已經退回到'while循環,並且您的原始整數檢查,以便您可以更輕鬆地將您的解決方案與我的比較。 – cdlane

+0

謝謝你的改變。現在更清楚了,因爲while循環是我學習的唯一循環,因爲它似乎適用於我所有的代碼。但除此之外,我會記住turtle.radians,嘗試清理我未來的代碼併爲發生的事情編寫描述。對未來的龜或甚至Python代碼有很好的學習經驗!但是,我仍然想知道第一個代碼的工作原理和第二個代碼的工作原理,儘管當我進行計算時他們都不應該工作。我不會將這個問題標記完成,直到有人能夠解釋爲什麼這樣。 –