英文:
The iterating polygons increasing by length of 10 px eachtime don't center perfectly with its inner polygon. What could the maths after line 11 be?
问题
为什么迭代的多边形不能完美对齐(如果我尝试创建一个具有4个边的多边形,它可以正常工作,但任何其他形状都会对齐有点不同)。这与第11行到第16行有关吗?
这是我尝试用我的函数解决的问题
import turtle
t = turtle.Turtle()
t.speed(5)
def draw_shape(length, sides, colores, times):
for timestotal in range(1, times+1):
for side in range(sides):
t.color(colores)
t.forward(length*timestotal)
t.right(360/sides)
t.penup()
t.back(length*2)
t.left(360/sides)
t.forward(length*2)
t.right(360/sides)
t.pendown()
draw_shape(4, 8, "red", 8)
只要所有的形状都像练习中那样居中,升序或降序的长度都无关紧要。
不幸的是,如果长度参数不是4,这些形状就不会正确对齐。
如果我在penup()和pendown()之间为每个从3到13的数字传递不同的命令,使用if、elif和else语句,它可以对齐这些形状,但每个数字(长度)都需要有自己的一组代码:
可能与这个命令有关:
t.goto(-(length*timestotal)/2, (length*timestotal)/2)
英文:
Why don't the iterating polygons not align perfectly. (if I try to make a polygon with 4 sides, it works fine, but any other shape and it aligns a bit differently). Is it something to do from line 11 to line 16?
This is the question I am trying to solve with my function
import turtle
t = turtle.Turtle()
t.speed(5)
def draw_shape(length, sides, colores, times):
for timestotal in range(1, times+1):
for side in range(sides):
t.color(colores)
t.forward(length*timestotal)
t.right(360/sides)
t.penup()
t.back(length*2)
t.left(360/sides)
t.forward(length*2)
t.right(360/sides)
t.pendown()
draw_shape(4, 8, "red", 8)
It doesn't really matter if it is ascending or descending lengths as long as all the shapes are centered as is in the exercise.
Unfortunately if the parameter is anything other than 4 the shapes do not align properly
Could it be something around this command:
t.goto(-(lengthtimestotal)/2,(lengthtimestotal)/2)
答案1
得分: 0
似乎问题可以简化为“从中心点绘制一个具有n
个边的正多边形”。如果你能做到这一点,那么你只需要在外循环中迭代不同的尺寸,都从相同的点(海龟当前的位置)绘制。
我不认为仅使用forward
、backward
和转向命令来从中心点绘制一个正多边形是容易的。但可以使用经典的三角函数方法来计算围绕圆圈的多边形的顶点:
import turtle
from math import cos, radians, sin
def draw_shape(length, color, sides, times):
t.color(color)
x, y = t.pos()
side = 360 // sides
for i in range(times):
current_length = length // times * (1 + i)
t.penup()
for angle in range(0, 361, side):
t.goto(
current_length * cos(radians(angle - side / 2)) + x,
current_length * sin(radians(angle - side / 2)) + y
)
t.pendown()
t = turtle.Turtle()
t.speed(5)
draw_shape(length=100, color="red", sides=8, times=5)
turtle.exitonclick()
angle - side / 2
部分用于将多边形旋转半个边,以匹配规范。
我还注意到 draw_shape(100, "blue", 4, 3)
的输出不太寻常。你可以通过 current_length = length - (20 * i)
来实现这一点,这是硬编码的步长。虽然不是很方便,但却是一种方法。
英文:
It seems like the problem can be reduced to "draw a regular polygon of n
sides from a center point". If you can do that, then you need only iterate with different sizes in the outer loop, all drawing from the same point (the turtle's current location).
I don't think it's easy to draw a regular polygon from a center point using only forward
, backward
and turning commands. But it's possible to use the classic trig polygon approach to compute the vertices of the polygon around the circle:
import turtle
from math import cos, radians, sin
def draw_shape(length, color, sides, times):
t.color(color)
x, y = t.pos()
side = 360 // sides
for i in range(times):
current_length = length // times * (1 + i)
t.penup()
for angle in range(0, 361, side):
t.goto(
current_length * cos(radians(angle - side / 2)) + x,
current_length * sin(radians(angle - side / 2)) + y
)
t.pendown()
t = turtle.Turtle()
t.speed(5)
draw_shape(length=100, color="red", sides=8, times=5)
turtle.exitonclick()
The angle - side / 2
bit is used to rotate the polygon by half a side to match the spec.
I also see draw_shape(100, "blue", 4, 3)
has unusual output. You can get this with current_length = length - (20 * i)
which hardcodes the step size. Not very pleasant to have to do, but such it is.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论