Мы можем выработать тригонометрию, чтобы сделать это, и есть примеры таких, если вы обращаетесь к SO.Вот один из подходов:
from turtle import Screen, Turtle
from math import pi, sin
def polygon(turtle, sides, length):
angle = 2 * pi / sides
radius = length / 2 / sin(angle / 2)
towards_center = angle / 2 + pi / 2
turtle.penup()
turtle.forward(radius)
turtle.left(towards_center)
turtle.pendown()
for _ in range(sides):
turtle.forward(length)
turtle.left(towards_center)
turtle.forward(radius)
turtle.backward(radius) # undo forward(radius)
turtle.right(towards_center) # undo turtle.left(towards_center)
turtle.left(angle)
screen = Screen()
yertle = Turtle()
yertle.radians() # switch turtle to radians to match math
yertle.dot() # mark the center of our screen for reference
polygon(yertle, 5, 100)
yertle.hideturtle()
screen.exitonclick()
![enter image description here](https://i.stack.imgur.com/wx53E.png)
Используя преимущества собственных возможностей черепахи, мы также можем сделать:
def polygon(turtle, sides, length):
angle = 2 * pi / sides
radius = length / 2 / sin(angle / 2)
turtle.penup()
turtle.sety(-radius)
turtle.pendown()
for _ in range(sides):
turtle.circle(radius, angle, steps=1)
heading = turtle.heading()
position = turtle.position()
turtle.home()
turtle.setposition(position)
turtle.setheading(heading)
![enter image description here](https://i.stack.imgur.com/eJmUu.png)