Python 用蟒蛇画花
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42265119/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
drawing flower with python turtle
提问by njathan
I am learning programming with python (referring think python 2
) and am struck at a program. Problem statement: Python program to draw a symmetric flower after seeking the size of and number of petals from user
.
我正在用 python 学习编程(指的是 think python 2
)并且对一个程序感到震惊。问题陈述:Python program to draw a symmetric flower after seeking the size of and number of petals from user
。
The code i came up with is below, except i am unable to get the angle between each petal mathematically right (part where the code near the end states bob.lt(360/petal)
). Can someone help here?
我想出的代码如下,除了我无法在数学上正确计算每个花瓣之间的角度(代码接近结束状态的部分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()
Correct (Symmetric)
Incorrect (Asymmetric)
正确(对称)
不正确(不对称)
回答by Anmol Gautam
Just modify your code like this(in draw_petals
add b.rt(360/petals-30
and correct bob.lt(360/petals)
to 360/4
):
只需像这样修改您的代码(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)
回答by cdlane
I think the problem is simpler than you're making it.
我认为这个问题比你做的更简单。
The first issue is that drawing a petal changes the turtle heading and you're trying to do math to set it back to where it started. Here we can just record the heading before drawing the petal and restore it afterward, no math.
第一个问题是,绘制花瓣会改变海龟的航向,而您正在尝试进行数学运算以将其设置回开始的位置。在这里,我们可以在绘制花瓣之前记录标题并在之后恢复它,无需数学。
The second issue is you're implementing your own arc code when turtle can do this using an extent argument to turtle.circle()
which produces the same result but much faster:
第二个问题是当海龟可以使用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()
USAGE
用法
> python3 test.py
What is the radius of the flower? 100
How many petals do you want? 10
OUTPUT
输出