2017-02-16 114 views
1

我正在學習用python編程(指的是想想python 2),並且對程序感到驚訝。問題說明:Python program to draw a symmetric flower after seeking the size of and number of petals from user用蟒蛇繪製花朵

我想出的代碼如下,除了我無法得到每個花瓣之間的數學角度正確的部分(代碼附近的代碼狀態bob.lt(360/petal))。有人可以幫忙嗎?

import math 
radius=int(input("What is the radius of the flower? ")) 
petals=int(input("How many petals do you want? ")) 
#radius=100 
#petals=4 


def draw_arc(b,r): #bob the turtle,corner-to-corner length (radius) of petal (assume 60 degree central angle of sector for simplicity) 
    c=2*math.pi*r #Circumference of circle 
    ca=c/(360/60) #Circumference of arc (assume 60 degree central angle of sector as above) 
    n=int(ca/3)+1 #number of segments 
    l=ca/n #length of segment 
    for i in range(n): 
     b.fd(l) 
     b.lt(360/(n*6)) 

def draw_petal(b,r): 
    draw_arc(b,r) 
    b.lt(180-60) 
    draw_arc(b,r) 

import turtle 
bob=turtle.Turtle() 

#draw_petal(bob,radius) 

for i in range(petals): 
    draw_petal(bob,radius) 
    bob.lt(360/petals) 

turtle.mainloop() 

Expected Flower 正確(對稱) Incorrect Flower 不正確的(非對稱)

+0

1)你可以鏈接到你導入的Turtle包的文件嗎? 2)你看到發生的結果和你期望的結果有什麼不同?即你怎麼知道你沒有在數學上得到花瓣之間的角度?這可能有助於顯示圖形。 – LarsH

+0

我已經用最後期望的輸出捕捉來更新原始問題......花應該是對稱的。官方文檔在這裏 - > https://docs.python.org/3.1/library/turtle.html – njathan

回答

2

只需修改這樣的代碼(在draw_petals添加b.rt(360/petals-30,正確bob.lt(360/petals)360/4):

import math 
radius=int(input("What is the radius of the flower? ")) 
petals=int(input("How many petals do you want? ")) 
#radius=100 
#petals=4 


def draw_arc(b,r): #bob the turtle,corner-to-corner length (radius) of petal (assume 60 degree central angle of sector for simplicity) 
    c=2*math.pi*r #Circumference of circle 
    ca=c/(360/60) #Circumference of arc (assume 60 degree central angle of sector as above) 
    n=int(ca/3)+1 #number of segments 
    l=ca/n #length of segment 
    for i in range(n): 
     b.fd(l) 
     b.lt(360/(n*6)) 


def draw_petal(b,r): 
    draw_arc(b,r) 
    b.lt(180-60) 
    draw_arc(b,r) 
    b.rt(360/petals-30) # this will take care of the correct angle b/w petals 


import turtle 
bob=turtle.Turtle() 
#draw_petal(bob,radius) 

for i in range(petals): 
    draw_petal(bob,radius) 
    bob.lt(360/4) 
+1

我沒有看到保留行'bob.lt(360/4)'而不是修復它的理由成爲'bob.lt(360/petals)' - 看起來你已經對'draw_petal()'中的代碼進行了過度糾正,以抵消和保存其他地方的錯誤,而不是實際糾正錯誤。 – cdlane

+0

感謝您指出@cdlane。我正在測試4片花瓣,而且你對我的矯正是正確的。我的意圖是使用'bob.lt(360 /花瓣)' – njathan

+0

謝謝@Tarptaeya。考慮到我在原帖中的錯誤(關於'bob.it(360/petals)',你的解決方案對於4個花瓣完美無缺......不多,不少,現在糾正原來的問題... – njathan

1

我認爲問題比你做得更簡單。

第一個問題是繪製花瓣會改變海龜的標題,並且您正在嘗試做數學運算以將其設置回開始位置。在這裏,我們可以在畫花瓣之前記錄標題並在之後恢復,不需要數學。

第二個問題是你實現自己的弧線代碼時,烏龜可以做到這一點使用的程度參數turtle.circle()產生相同的結果,但要快得多:

from turtle import Turtle, Screen 

def draw_petal(turtle, radius): 
    heading = turtle.heading() 
    turtle.circle(radius, 60) 
    turtle.left(120) 
    turtle.circle(radius, 60) 
    turtle.setheading(heading) 

my_radius = int(input("What is the radius of the flower? ")) 
my_petals = int(input("How many petals do you want? ")) 

bob = Turtle() 

for _ in range(my_petals): 
    draw_petal(bob, my_radius) 
    bob.left(360/my_petals) 

bob.hideturtle() 

screen = Screen() 
screen.exitonclick() 

用法

> python3 test.py 
What is the radius of the flower? 100 
How many petals do you want? 10 

輸出

enter image description here

+0

親愛的@cdlane,OP doesn' t想要替代解決方案,他只想糾正其原始代碼 – Tarptaeya

+0

@OP,對原始代碼的修正是'draw_petal()'中的'heading = turtle.heading()'和'turtle.setheading(heading)'。是完全正確的,應該是被接受的答案。其他改進也是有價值的。 – LarsH