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?
Question:
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)
Answers:
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.
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)
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.