我有一些代码是用户输入他们想要他们的明星的长度然后它抽出明星。我在这里要完成的是,每次他们输入的不仅仅是它吸引了明星,而且还让它在屏幕中居中。所以中间点0,0
import turtle
Length = eval(input("enter the length you want for your star: "))
turtle.penup()
turtle.goto(0,200)
turtle.pendown()
turtle.goto(0,-200)
turtle.penup()
turtle.goto(200,0)
turtle.pendown()
turtle.goto(-200,0)
turtle.penup()
turtle.goto(0,0)
turtle.showturtle
turtle.pendown()
turtle.left(36*4)
turtle.forward(Length)
turtle.right(36*4)
turtle.forward(Length)
turtle.right(36*4)
turtle.forward(Length)
turtle.right(36*4)
turtle.forward(Length)
turtle.right(36*4)
turtle.forward(Length)
答案 0 :(得分:2)
试试这个
import turtle
import math
theta = 18 * math.pi / 180 # convert degrees to radians
Length = eval(input("enter the length you want for your star: "))
x = math.sin(theta) * Length
y = math.cos(theta)* Length / 2
turtle.penup()
turtle.goto(0,200)
turtle.pendown()
turtle.goto(0,-200)
turtle.penup()
turtle.goto(200,0)
turtle.pendown()
turtle.goto(-200,0)
turtle.penup()
#turtle.goto(0,0)
turtle.goto(x,-y)
turtle.showturtle
turtle.pendown()
turtle.left(36*4)
turtle.forward(Length)
turtle.right(36*4)
turtle.forward(Length)
turtle.right(36*4)
turtle.forward(Length)
turtle.right(36*4)
turtle.forward(Length)
turtle.right(36*4)
turtle.forward(Length)
turtle.hideturtle()
input("Press Enter to exit")
只需要弄清楚你正在绘制的恒星中心的坐标,然后用该向量平移起始位置,将中心移动到原点。
编辑:
如果您回到原始图形,在y轴的左侧,您会看到两个线段,呈倒V形。如果我们将这些线段的两个交点与x轴相互连接起来,我们就有一个等腰三角形,其中心是恒星的中心。这是我们需要找到的重点。现在,恒星的每个角度都是36度,一半是18度。这是18来自哪里。要真正找到中心,我们需要使用一些三角函数,我收集你尚未研究过。 sin和cos函数是三角函数的正弦和余弦函数。这些函数的参数通常不是以度为单位,而是在另一个称为弧度的系统中。碰巧1800度与pi弧度相同,因此theta只是一个18度角,以弧度为单位。
"为什么18度,"我听到你问了吗?请记住,我们试图找到等腰三角形的中心,两边等于长度,斜边是长度。所以,我们垂直于基座,我们将它切成两个直角三角形,一个锐角为18度(另一个为72度)。这就是正弦和余弦进来的地方。在一个正三角形中斜边长度,与锐角θ相对的边长度为sin(theta)*Length
,与角度θ相邻的边长度为cos(theta)*Length
。
这是一个冗长的解释,但我不知道如何缩短这种格式。你可以找到图片here
的解释