Python 有没有办法检查多边形是否完全闭合?

Python 有没有办法检查多边形是否完全闭合?,python,turtle-graphics,Python,Turtle Graphics,我正试图画一个红色的停车标志,用白色勾勒出来。然而,我似乎不明白为什么红色八角形没有填充 如果八角形仍然打开,这就是为什么它没有填充的原因吗?如果是,我如何检查它是否打开 import turtle turtle.bgcolor("black") turtle.penup() turtle.goto(-104,-229) # Draws white octagon outline for i in range(8): turtle.pensize(10) turtle.color

我正试图画一个红色的停车标志,用白色勾勒出来。然而,我似乎不明白为什么红色八角形没有填充

如果八角形仍然打开,这就是为什么它没有填充的原因吗?如果是,我如何检查它是否打开

import turtle
turtle.bgcolor("black")
turtle.penup()
turtle.goto(-104,-229)
# Draws white octagon outline
for i in range(8):
    turtle.pensize(10)
    turtle.color("white")    
    turtle.pendown()
    turtle.forward(193)
    turtle.right(5)
    turtle.left(50)

# Draws red octagon
turtle.penup()
turtle.goto(-100,-220)
for i in range(8):
    turtle.pensize(10)
    turtle.color("red")
    turtle.fillcolor("red")
    turtle.begin_fill()    
    turtle.pendown()
    turtle.forward(185)
    turtle.right(5)
    turtle.left(50)
    turtle.end_fill()

# Writes "STOP"
turtle.penup()   
turtle.goto(5,-50)
turtle.setheading(360 / 8 / 2)
turtle.pendown()
turtle.stamp()  
turtle.pencolor("white")
turtle.shapesize(0.9)
turtle.stamp()
turtle.shapesize(1.0)
turtle.write("STOP", align="center", font=("Arial",110,"normal"))   
turtle.done()

您需要将开始和结束填充放在循环之外,因为它一次只填充一行

# Draws red octagon
turtle.penup()
turtle.goto(-100,-220)
turtle.pensize(10)
turtle.color("red")
turtle.fillcolor("red")
turtle.begin_fill()
for i in range(8):
    turtle.pendown()
    turtle.forward(185)
    turtle.right(5)
    turtle.left(50)
turtle.end_fill()

我发现你的代码有两个问题。首先,将
begin\u fill()
end\u fill()
放在octogon循环中——它们应该环绕循环的外部,而不是循环的一部分。其次,您通常会使事情变得更加复杂,包括抛出与结果无关的代码(例如
stamp()
shapesize()
setheading()
,等等)。下面是修复填充后代码的简化返工:

from turtle import Screen, Turtle

SIDE = 200
PEN_SIZE = 10
FONT_SIZE = 150
FONT = ("Arial", FONT_SIZE, "bold")

screen = Screen()
screen.bgcolor("black")

turtle = Turtle()
turtle.hideturtle()
turtle.pensize(PEN_SIZE)
turtle.penup()

# Draw white octagon with red fill
turtle.goto(-SIDE/2, -SIDE/2 + -SIDE/(2 ** 0.5))  # octogon geometry
turtle.color("white", "red")  # color() sets both pen and fill colors
turtle.pendown()

turtle.begin_fill()
for _ in range(8):
    turtle.forward(SIDE)
    turtle.left(45)
turtle.end_fill()

turtle.penup()

# Write "STOP"
turtle.goto(0, -FONT_SIZE/2)  # roughly font baseline
turtle.pencolor("white")

turtle.write("STOP", align="center", font=FONT)  # write() doesn't need pendown()

screen.exitonclick()