问题描述
我正在尝试用 Python 创建一个程序来绘制 n 尖星。我遇到了有 6、14、22、... 点的星星的问题。我不知道如何解决该模式。
这是我目前所拥有的:
from turtle import *
# Settings
speed(5)
width(1)
color('red','yellow') # outline,fill
x=int(input('Enter the number of points you want the star to have.'))
begin_fill()
# If a star has even number of points,then it needs to be drawn differently
if x%4 == 0:
for i in range(x):
y=180-360/x
right(y)
forward(300)
y+=360/x
# For special cases such as 10,18,26,...
elif x%2 == 0:
for i in range(x):
y=90+180/x
right(y)
forward(300)
# For special cases such as 6,14,22,...
# I need help here (this condition needs to be changed)...
elif x%2 == 0:
for i in range(x):
y=2*180/x
right(y)
forward(300)
# For stars with odd number of points
else:
for i in range(x):
right(180-180/x)
forward(300)
end_fill()
hideturtle()
exitonclick()
解决方法
如果您的问题只是条件问题,那么也许您应该使用列表/元组
elif x in (10,18,26):
elif x in (6,14,22):
但这不会检查所有可能的值
因此您将需要更复杂的比较,但我看不到任何模式来识别 10,26
或 6,22
- 所有这些都被 2
除以使用 x % 2
没用。
编辑:
我看到了模式
对于列表 6,22
,我看到
6 + 8 = 14
14 + 8 = 22
或者其他方式
6 = 6 + (8*0)
14 = 6 + (8*1)
22 = 6 + (8*2)
这给出了 if (x-6) % 8 == 0: ...
或更简单的 if x % 8 == 6: ...
对于列表 10,26
,我看到
10 + 8 = 18
18 + 8 = 26
或者其他方式
10 = 10 + (8*0)
18 = 10 + (8*1)
26 = 10 + (8*2)
这给出了 if (x-10) % 8 == 0: ...
或更简单的 if x % 8 == 10: ...
您使用一个多边形(一个 for
循环)绘制星星,但星星 6 是由两个分离的三角形(两个多边形)创建的,因此需要不同的绘制方法。
但是如果您只想使用一个 for
循环进行绘制,那么您应该只绘制外部线(边框)- 然后您不必选中 x % 2
。
此代码为任何值绘制星星
from turtle import *
# Settings
speed(5)
width(1)
color('red','yellow') # outline,fill
#x = int(input('Enter the number of points you want the star to have.'))
x = 6
begin_fill()
angle = (360/x)
print(x,angle)
for _ in range(x):
forward(50)
left(angle)
forward(50)
right(2*angle) # double angle
end_fill()
hideturtle()
exitonclick()
star 6
star 14
编辑:
使用 for
循环绘制从 14 到 5 的代码
from turtle import *
# Settings
speed(5)
width(1)
color('red',fill
#x = int(input('Enter the number of points you want the star to have.'))
for x in range(14,4,-1):
begin_fill()
angle = (360/x)
print(x,angle)
for _ in range(x):
forward(50)
left(angle)
forward(50)
right(2*angle) # double angle
end_fill()
hideturtle()
exitonclick()
结果:
它表明星星有不同的大小,因此代码必须为 forward()
计算不同的值,但此时我跳过了这个问题。首先,我必须在纸上进行计算。
编辑:
我曾经使用两个star 6
(triangles
和red
)绘制green
的代码
from turtle import *
# --- functions ---
def triangle(width,outline):
color(outline)
for _ in range(3):
forward(width)
right(180-60)
# --- main ---
width(2)
w = 150
triangle(w,'red')
up()
forward(w//2)
left(90)
forward( (w//3 * (3**0.5)) / 2 ) # triangle: h = (a * sqrt(3)) / 2
right(90+(360/6))
down()
triangle(w,'green')
hideturtle()
exitonclick()
,
另一种通用方法是计算所有角点的坐标,然后通过 goto()
调用将它们连接起来。请注意,所有相关点都位于两个同心圆上。我recently answered一个关于如何计算圆上规则间隔点的问题,因此我们可以在此处使用该代码:
from turtle import *
import math
# Settings
speed(5)
width(1)
color('red',fill
r_inner = 50 # inner radius
r_outer = 150 # outer radius
n = int(input('Enter the number of points you want the star to have.'))
# Calculate coordinates of the points on the inner and outer circle
points_x_inner = []
points_y_inner = []
points_x_outer = []
points_y_outer = []
for i in range(n):
angle = 2 * math.pi * i / n
points_x_inner.append(r_inner * math.cos(angle))
points_y_inner.append(r_inner * math.sin(angle))
angle2 = angle + math.pi / n
points_x_outer.append(r_outer * math.cos(angle2))
points_y_outer.append(r_outer * math.sin(angle2))
# Connect the points with lines
penup()
goto([points_x_outer[-1],points_y_outer[-1]])
pendown()
begin_fill()
for x_in,y_in,x_out,y_out in zip(points_x_inner,points_y_inner,points_x_outer,points_y_outer):
goto([x_in,y_in])
goto([x_out,y_out])
end_fill()
hideturtle()
exitonclick()