-1

I have some code were the users input the length they want their star to be and then it draws out the star. What I am trying to accomplish here, is that every-time they make their input not only that it draws the star but it also keeps it centered in the screen. so middle point 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)

1 Answers1

2

Try this

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")

It's just a matter of figuring out the coordinates of the center of the star you were drawing, and then translating the starting position by that vector, to move the center to the origin.

EDIT:

If you go back to your original drawing, to the left of the y-axis, your see two line segments, in an inverted V-shape. If we join the two intersections of these segments with the x-axis to each other, we have an isosceles triangle whose center is the center of the star. That's the point we need to find. Now, each angle of the star is 36 degrees, and half that is 18. That's where the 18 comes from. To actually find the center, we need to use some trigonometry, which I gather you haven't studied yet. The functions sin and cos are the sine and cosine functions from trigonometry. The arguments to these functions are usually given not in degrees, but in another system called radians. It happens that 1800 degrees is the same as pi radians, so theta is just an 18 degree angle, measured in radians.

"Why 18 degrees," did I hear you ask? Remember that we're try to find the center of that isosceles triangle, with two side equal to length, and a hypotenuse of Length. So, we I drop a perpendicular to the base, we cut it into two right triangles, with one acute angle of 18 degrees (and the other 72 degrees.) And this is where the sine and cosine come in. In a right triangle with hypotenuse Length, the side opposite an acute angle theta has length sin(theta)*Length and the side adjacent to the angle theta has length cos(theta)*Length.

This is a long-winded explanation, but I don't know how to make it shorter in this format. You can find an explanation with pictures here

saulspatz
  • 4,309
  • 4
  • 28
  • 38
  • is that the actual formula for theta? my math is not to good so I'm trying to understand the variable theta. Also .sin function & .cos function what would be their purpose? sorry for questions I'm learning. – Kenneth Luiggi Sep 09 '15 at 17:17
  • The explanation is far to long too fit in a comment, so I edited my post. BTW: If my answer solved your problem, please accept it. – saulspatz Sep 09 '15 at 19:46
  • No, click on the check mark. Anyone can upvote any question or answer, but only the original proposer can accept an answer. – saulspatz Sep 10 '15 at 02:24