海龟绘图,如何画一颗星星?

4

我想画一个填充的星形,例如:

http://www.seaviewstickers.co.uk/shop/media/catalog/product/cache/1/thumbnail/600x600/9df78eab33525d08d6e5fb8d27136e95/b/l/black_11.jpg

到目前为止,我的代码是这样的:

def draw_star(size,color):
    count = 0
    angle = 144
    while count <= 5:
        turtle.forward(size)
        turtle.right(angle)
        count += 1
    return

draw_star(100,"purple")

我想用传递的函数颜色来填充星星。我该怎么做?

你有一个偏移一位的错误。其中一边会被绘制两次。 - John La Rooy
11个回答

4

要画一个五角星,每边需要画两条线。每个角度需要加起来等于72度(360/5)。

import turtle
def draw_star(size, color):
    angle = 120
    turtle.fillcolor(color)
    turtle.begin_fill()

    for side in range(5):
        turtle.forward(size)
        turtle.right(angle)
        turtle.forward(size)
        turtle.right(72 - angle)
    turtle.end_fill()
    return

draw_star(100, "purple")

尝试不同的角度值,以获得所需的形状。


我喜欢这个解决方案的外观,但是我对size参数有疑问——它与什么相关?例如,我认为size是一个圆的半径,星星应该适合其中。然而,在这个实现中,图像的大小随着anglesize的变化而改变,因此很难正确调整大小。 - cdlane
我在turtle end_fill文档中添加了一条注释,指出重叠区域可能会或可能不会被填充,这取决于操作系统图形、重叠的性质和重叠数量。 - Terry Jan Reedy

1
海龟文档中搜索“fill”:
def draw_star(size,color):
    count = 0
    angle = 144
    turtle.fillcolor(color)
    turtle.begin_fill()
    for _ in range(5):
        turtle.forward(size)
        turtle.right(angle)
    turtle.end_fill()

draw_star(100,"purple")

注意:这里的return语句是必要的,通过像这样编写循环,它不会重复绘制轮廓。


在某些系统上,这会出现@KonradRudolph在他的答案中描述的“交替”填充问题。 - cdlane

1
def draw_star(size, color):
...:     turtle.reset()
...:     turtle.color(color)
...:     turtle.fillcolor(color)
...:     turtle.begin_fill()
...:     turtle.lt(260)
...:     for _ in range(5):
...:         turtle.fd(size)
...:         turtle.lt(170)
...:         turtle.fd(size)
...:         turtle.rt(100)
...:     turtle.end_fill()
...:     
...:draw_star(size, color)

这跟 OP 的星星例子完全不一样。fillcolor()调用与前面的.color()调用重复了。请在您的答案中解释此解决方案为现有讨论添加了什么内容。 - cdlane

0

我匆忙地完成了这个,它可以更加完善。欢迎提出更好的解决方案 :)

import turtle 

x = turtle.Turtle()
x.speed(0)

 def draw_star(length, angle):

 for i in range(5): #the star is made out of 5 sides
  x.forward(length)
  x.right(angle)


for i in range(1):
 x.color("purple") #if  you want outline of the star choose .color("purple" + "outline color")
 x.begin_fill()
 draw_star(100, 144) #144 is the perfect angle for a star
 x.end_fill()

0

尝试使用turtle.fill进行实验。但是,如果您在代码中仅使用它而没有进一步更改,您将得到一个“交替”填充:

alternating fill

您需要调整例程以绘制星形的轮廓,避免交叉线(可以通过在两个角度之间交替进行绘制来实现),或单独填充星形内部(通过跟踪内接多边形来实现)。


不给我交替填充。 - poke
@poke 它绝对应该可以,看看我的截图。Eric的答案中的代码也是这样做的。 - Konrad Rudolph
好的,我使用了OP的代码,并在开头添加了“turtle.fill(True)”,也使用了Eric的代码,但两者都没有给我这个结果。它们都显示线穿过星形,但填充是完整的。 - poke
@poke 很有趣:这可能取决于系统如何实现海龟图形。我正在使用OS X和它的libtk。我假设在不同的系统上,TK实现此操作的方式肯定是不同的。 - Konrad Rudolph
可能。我用的是Windows系统,尝试过Python 2.7和3.4两个版本。 - poke

0

也许可以尝试这个:

turtle.write("★", font=("Arial", 40, "normal"))

这只是针对海龟绘图的编写,可能并没有什么帮助,但你可以尝试一下。


0
from turtle import *
hideturtle()

def draw_star(sidesize, points, alfacorner, color):
    betacorner = 360/points+alfacorner
    fillcolor(color)
    begin_fill()
    for x in range(points*2):
        forward(sidesize)
        if x % 2 == 0:
            left(180-alfacorner)
        else:
            right(180-betacorner)
    end_fill()


draw_star(70, 8, 90, 'grey')
draw_star(90, 5, 72, 'yellow')
draw_star(120, 5, 36, 'red')
draw_star(65, 6, 60, 'darkblue')
draw_star(80, 4, 45, 'darkviolet')
draw_star(80, 3, 30, 'greenyellow')

exitonclick()

0
def create_star (pointer, color_mix, central_point_x_value,\
                    central_point_y_value, length, height):
    travel_distance = length * .223
    pointer.home() # resets the orientation of the pointer
    #   without reseting everything
    pointer.penup()

    pointer.goto (central_point_x_value,central_point_y_value)
    pointer.left(90)
    pointer.forward(height) #move to the top of the star
    pointer.left(18) # must straighten the direction to match the iteration needs
    #draw Star , going counterclockwise
    # starting at the top and ending at the top of each outside triangle
    pointer.pendown()
    pointer.fillcolor (color_mix)
    pointer.begin_fill()
    count = 5
    while count != 0:
        #draw the star
        pointer.left(144)
        pointer.forward(travel_distance)
        pointer.right(72)
        pointer.forward(travel_distance)

        count -=1
    pointer.end_fill()

你能解释一下你的参数吗?如果我在屏幕中央创建一个半径为50的圆,然后执行:create_star(turtle.Turtle(), "purple", 0, 0, 100, 100),我期望在屏幕中央得到一个“填充”我的圆的星形图案,但实际上我得到的是一个大小只有我的圆的一半,且位于我的圆上方的星形图案。 - cdlane

0

如果你在使用Windows系统,可能可以采用以下方法:

turtle.color("purple")
turtle.begin_fill()
turtle.circle(100, extent=720, steps=5)
turtle.end_fill()

然而,这段代码存在两个问题:它不是与您的插图相同的样式星形;它在所有Python turtle/tkinter实现中的表现不一致(有些只显示部分填充):

enter image description here

这里有一个使用印章而不是绘图的替代实现,应该可以解决这两个问题:

STAR_SIZE = 100

EXPANSION = 1.2
TRANSLATION = STAR_SIZE * EXPANSION / 4

turtle.hideturtle()
turtle.color("purple")
turtle.shape("triangle")
turtle.turtlesize(STAR_SIZE * EXPANSION / 20)

for _ in range(5):
    turtle.right(72)
    turtle.forward(TRANSLATION)
    turtle.stamp()
    turtle.backward(TRANSLATION)

enter image description here


0

这应该可以工作,随时问问题!

def draw_star(size,color):
    count = 0
    angle = 144
    turtle.color(color)
    turtle.begin_fill()
    while count <= 5:
        turtle.forward(size)
        turtle.right(angle)
        count += 1
    turtle.end_fill()
    return

draw_star(100,"purple")

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接