4

This is my code:

import turtle

bob = turtle.Turtle()   # Creating the object "bob"

def draw_circle(t):
    for i in range (360):   # Loop for drawing a circle
        t.fd(1) # Draw a line with the length of 1 pixel in the forward direction
        t.lt(1) # Turn 1 degree to the left

draw_circle(bob)
turtle.mainloop()

Which results in the following drawing:

enter image description here

So my questions is how can I measure the radius of this circle (in pixels), knowing that the circle was drawn by the repetition of drawing a 1 pixel line followed by a 1 degree turn 360 times?

1
  • 1
    If we can assume each segment is 1 pixel in length that means the circumference is 360pixels, that means the radius is 360 = 2 * π * r so r = 180 / π Apr 11, 2016 at 16:39

2 Answers 2

1

You have a couple of options.

The circumference of the circle equals (roughly) the step size times the number of steps, that is 360 pixels.

So the radius = 360 / (2 * pi)

Alternatively, use bob.pos to get the turtle's coordinates at the start of the circle and when it's done 180 steps, since those points will be at opposite ends of a diameter of the circle.

The simple way to do that is to break your loop up into two loops, with each loop drawing half the circle.

You can use Pythagoras theorem to find the distance between those two points.

1
  • 1
    Since turtles learn about Pythagoras in school, you can simply do bob.distance(0, 0) (or whereever Bob starts his journey) after travelling the half circle.
    – cdlane
    Oct 9, 2018 at 0:13
0

The radius of your "circle" should be 57.28996163075943 pixels and can be calculated like so:

import math
radius = math.sin(math.radians(89)) / math.sin(math.radians(1))
print('Radius of circle =', radius)

The reason this is true stems from the fact that your circle is not really a circle. It is composed of 360 different triangles. In particular, they are all ASA (angle, side, angle) triangles. Let us assume that when beginning to draw your circle, your turtle starts on a tangent of the circle. That means that if you were to draw a line that passed through the center of the circle you are drawing and the turtle that is making the drawing, your turtle's direction would be perpendicular to that line. Therefore, we can say the following:

angle_a = 90

We are saying that the first angle we know anything about in our triangle is 90 degrees. The next piece of information we need is the length of a side. Since the turtle draws a line of 1 pixel, we know that is the length and can add that to our notes:

angle_a = 90
side_c = 1

The final thing we need to know is one more angle. Now this has to be derived from previous knowledge since it may not be obvious at first. At the beginning of each drawing step, our turtle is parallel to a tangent on the circle; and that means that after turning the turtle by 1 degree, it is ready to begin the next drawing step and must be on a tangent. Therefore, at the end of a drawing step, it should be possible to trace a line back to the middle of the circle by following a path perpendicular to the direction the turtle is facing. Since a turn of 1 degree was made, we know that the other angle of the triangle we are making must be 89 degrees.

angle_a = 90
side_c = 1
angle_b = 89

From this we can conclude that the other angle in the triangle at the center of the circle must be 1 degree.

angle_a = 90
side_c = 1
angle_b = 89
angle_c = 1

Now that we have all this information, we can use the law of sines to find out the lengths of the other two sides of the triangle. We will get two different values. The first is the length of side a, the hypotenuse; and the second is the length of side b, the radius. You will notice that the longer length is how far away from the center of the circle the turtle is after its first drawing step. For purposes of calculating the radius, I chose to go with the second measurement which is barely any different.

It is time to solve some formulas:

side_a / sin(angle_a) = side_c / sin(angle_c)
side_a / sin(90) = 1 / sin(1)
side_a = sin(90) / sin(1)

side_a = math.sin(math.radians(90)) / math.sin(math.radians(1))  # hypotenuse

side_b / sin(angle_b) = side_c / sin(angle_c)
side_b / sin(89) = 1 / sin(1)
side_b = sin(89) / sin(1)

side_b = math.sin(math.radians(89)) / math.sin(math.radians(1))  # radius

You may also choose to average the lengths of sides a and b to get a better approximation of your circle's radius:

print('Radius of circle =', (side_a + side_b) / 2)

From this, you will get 57.29432506465481 as your approximate radius in pixels.


Addendum

After doing a little more research into the matter, it was easy to develop a function to help solve problems like this more easily in the future. Please realize that polygons do not necessarily have a diameter, but even-sided ones have at least two radiuses that are parallel with each other. Getting the radius with the following function is as easy as dividing its result by two. Example usage is included:

import math


def get_polygon_diameter(side_length, side_rotation):
    return side_length / math.sin(math.pi * side_rotation / 360)


length_of_each_side = 1  # in pixels
rotation_per_side = 1    # in degrees
diameter = get_polygon_diameter(length_of_each_side, rotation_per_side)
radius = diameter / 2
print(f'Radius of "circle" that is actually a polygon = {radius:.3f} pixels')

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.