我用乌龟得到一个简单的数字。但是问题是我不知道如何将这个数字放在圆圈内。 代码:
import turtle
painter = turtle.Turtle()
painter.pencolor("blue")
for i in range(50):
painter.forward(100)
painter.left(123*2)
painter.circle(70)
turtle.done()
答案 0 :(得分:0)
您需要将乌龟移动到正确的起始位置。请注意,这不是圆的中心!它从最右边的位置开始绘制圆-即,如果您想要一个半径为70的圆围绕(0,0),然后移动到(70,0),例如:
painter.penup()
painter.goto(70,0)
painter.pendown()
painter.circle(70)
仅供参考:我无法立即确定您图形的中心在哪里,但我怀疑它不在(0,0)处。在所有情况下,都应将海龟放置在形状中心的右侧,并偏移圆的半径,以使圆围绕它旋转。
答案 1 :(得分:0)
我的脑袋有点三角,我想出了这个角度。不知道我的半径是否正确。理想情况下,可以找出中心的坐标,但是快速而又肮脏的解决方案是:
import turtle
painter = turtle.Turtle()
painter.pencolor("blue")
for i in range(50):
painter.forward(100)
painter.left(123*2)
painter.right(123)
painter.right(90)
painter.penup()
painter.forward(10)
painter.left(90)
painter.pendown()
painter.circle(70)
turtle.done()
答案 2 :(得分:0)
另一种方法是对任意图像的位置求平均值,然后将该平均值用作周围圆圈的中心:
from turtle import Screen, Turtle, Vec2D
CIRCLE_RADIUS = 70
POLYGON_LENGTH = 100
POINTS = 50
screen = Screen()
painter = Turtle()
painter.speed('fastest')
painter.pencolor("blue")
total = Vec2D(0, 0)
for _ in range(POINTS):
painter.forward(POLYGON_LENGTH)
total += painter.position()
painter.left(246)
x, y = total * (1.0 / POINTS) # Vec2D can multiply by scalar but not divide
painter.penup()
painter.goto(x, y - CIRCLE_RADIUS)
painter.setheading(0)
painter.pendown()
painter.circle(CIRCLE_RADIUS)
screen.exitonclick()