[英]Draw polygon in polygons (regular polygons)
This is my code that draws regular polygons:这是我绘制正多边形的代码:
import turtle
tr = turtle.Turtle()
tr.lt(150)
for x in range(3,13):
for i in range(x):
tr.fd(80)
tr.lt(360//x)
turtle.done()
This is my output:这是我的输出:
But my expected output is:但我的预期输出是:
Can you help me?你能帮助我吗?
As already pointed in the comments正如评论中已经指出的那样
You probably want to move the turtle at thee end of the polygon, so the ends won't be connected您可能想将乌龟移动到多边形的末端,这样末端就不会连接
Looks like you need to calculate the radius offset and move in/out from the center if you want them all concentric.看起来你需要计算半径偏移量并从中心移入/移出,如果你想让它们都同心的话。 Currently you're always starting the first/last vertex at the same point.
目前,您总是在同一点开始第一个/最后一个顶点。
After having a quick look in the turtle methods在快速浏览了turtle 方法之后
I came up with this我想出了这个
import turtle
import math
tr = turtle.Turtle()
r0 = 20
tr.lt(150)
for x in range(3,13):
points = [
(r0 * (x-1) * math.cos(k*2*math.pi/x),
r0 * (x-1) * math.sin(k*2*math.pi/x))
for k in range(1,x+1)
]
tr.penup() # avoid creating a line connecting two polygons
tr.goto(*points[-1])
tr.pendown() # draw one polygon
for tx, ty in points:
tr.goto(tx,ty);
turtle.done()
PS.: I have never heard about this module before, and it was a surprise to see that it was installed in my machine. PS.:我以前从来没有听说过这个模块,看到它安装在我的机器上,我感到很惊讶。
A simple-minded solution using circle()
to do our work for us:一个简单的解决方案使用
circle()
为我们完成我们的工作:
import turtle
from math import pi
for sides in range(3, 13):
radius = 40 * sides / pi
turtle.penup()
turtle.sety(-radius)
turtle.pendown()
turtle.circle(radius, steps=sides)
turtle.hideturtle()
turtle.done()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.