首页 > 解决方案 > 使用海龟图形绘制 n 角星

问题描述

我正在尝试在 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()

标签: pythonturtle-graphicspython-turtle

解决方案


如果您的问题只是条件,那么也许您应该使用列表/元组

elif x in (10, 18, 26):

elif x in (6, 14, 22):

但这不会检查所有可能的值

因此,您将需要更复杂的比较,但我看不到任何要识别的模式,10,18,26或者6,14,22-所有这些都被除以,2因此使用x % 2毫无用处。

编辑:

我看到模式

对于6,14,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,18,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-loop 进行绘制,那么您应该只绘制外部线(边框) - 然后您不必检查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-loop 从 14 绘制到 5 的代码

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.')) 
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(redgreen)绘制的代码

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

推荐阅读