Python 海龟图形六边形定心
我需要一些帮助。我想把六边形放在大六边形的中心,但我现在不知道怎么做。下面是我的源代码和输出的图像链接Python 海龟图形六边形定心,python,turtle-graphics,Python,Turtle Graphics,我需要一些帮助。我想把六边形放在大六边形的中心,但我现在不知道怎么做。下面是我的源代码和输出的图像链接 import turtle polygon = turtle.Turtle() num_sides = 6 side_length = 20 move_left = 60 polygon.pensize(2) polygon.pencolor((245, 176, 66)) for turtle_move in range(num_sides): polygon.forw
import turtle
polygon = turtle.Turtle()
num_sides = 6
side_length = 20
move_left = 60
polygon.pensize(2)
polygon.pencolor((245, 176, 66))
for turtle_move in range(num_sides):
polygon.forward(side_length)
polygon.left(move_left)
polygon.penup()
polygon.left(2)
polygon.pendown()
side_length2 = 40
move_left2 = 60
我想把六边形放在大六边形的中心,但我不知道怎么做
for turtle_move in range(num_sides):
polygon.forward(side_length2)
polygon.left(move_left2)
以下是输出:
如果您阅读了有关六边形几何的内容,可以通过多种方法来实现,例如: 另一种方法是使用turtle
circle()
方法绘制六边形,然后将两个圆居中:
from turtle import Screen, Turtle
NUM_SIDES = 6
SIDE_LENGTH = 20
circumradius = SIDE_LENGTH
screen = Screen()
turtle = Turtle()
for _ in range(2):
turtle.penup()
turtle.sety(-circumradius)
turtle.pendown()
turtle.circle(circumradius, steps=NUM_SIDES)
circumradius *= 2
turtle.hideturtle()
screen.exitonclick()
我假设您使用的是
pencolor((245176,66))
您使用的是Repl.it或其他非标准Python turtle实现,因此您可能需要稍微调整上面的示例以适应您的环境。在开始绘制较小的示例之前,向左拐120,沿侧向长度1-侧向长度2前进,然后右转60度
from turtle import Screen, Turtle
NUM_SIDES = 6
SIDE_LENGTH = 20
circumradius = SIDE_LENGTH
screen = Screen()
turtle = Turtle()
for _ in range(2):
turtle.penup()
turtle.sety(-circumradius)
turtle.pendown()
turtle.circle(circumradius, steps=NUM_SIDES)
circumradius *= 2
turtle.hideturtle()
screen.exitonclick()