banner



How To Draw A Rotating Pentagon In Python Turtle

Here is the solution I came up with. Information technology is based on this diagram:

enter image description here

Math background

My solution uses "trigonometry", which is a method for calculating the length of one side of a triangle from the length of some other side and the angles of the triangle. This is advanced math which I would expect to be taught maybe in 9th or 10th grade. I do not expect someone in 5th grade to know trigonometry. Too I cannot explicate every particular of trigonometry, because I would have to write a lot and I do not think I take the teaching skills to brand it clear. I would recommend yous to expect at for case this video to learn almost the method:

https://world wide web.youtube.com/watch?v=5tp74g4N8EY

Yous could likewise ask your teacher for more than data, or enquiry almost it on the net on your ain.

Stride 1: Calculating the angles

We tin can do this without trigonometry.

Offset, we see there is a "pentagon" (5-sided polygon) in the middle. I desire to know the inner angle of a corner in this "pentagon". I telephone call this bending X:

enter image description here

How can nosotros calculate the angle X? We commencement recall that the sum of the inner angles in a triangle is 180°. We meet that we can divide a five-sides polygon into 5-ii triangles like this:

enter image description here

The sum of the inner angle of each of these 5-2 triangles is 180°. So for the whole 5-sided polygon, the sum of the inner angles is 180° * (5-2). Since all angles have the same size, each angle is 180°*(5-2) / v = 108°. So we have X = 108°.

The bending on the other side is the same as X. This allows us the calculate the bending between the ii X. I volition call this bending Y:

enter image description here

Since a full circle is 360°, we know that 360° = ii*X + 2*Y. Therefore, Y = (360° - 2*X) / ii. Nosotros know that X = 108°, so we become Y = 72°.

Side by side, nosotros come across there is a triangle containing the Y angle. I desire to know the angle Z at the other corner of the triangle:

enter image description here

The inner angles of a triangle sum up to 180°*(iii-2) = 180°. Therefore, we know that 180° = 2*Y + Z, and so Z = 180° - 2*Y. Nosotros know that Y = 72°, so we become Z = 36°.

Nosotros will utilize the angle Z a lot. You can come across that every corner of the green star has angle Z. The blue star is the same as the dark-green star except information technology is rotated, so all blueish corners besides have angle Z. The corners of the red star are twice as broad as the corners of the light-green and blue stars, so the corners of the red star accept the angle 2*Z.

Step ii: Calculating the lengths

First, nosotros find that all outer corners are on a circumvolve. Nosotros call the radius of this circle R. We exercise non take to calculate R. Instead, we can take any value we desire for R. We volition e'er get the aforementioned shape but in unlike sizes. We could call R a "parameter" of the shape.

enter image description here

Given some value for R, I desire to know the following lengths:

enter image description here enter image description here enter image description here enter image description here enter image description here enter image description here enter image description here enter image description here enter image description here enter image description here

Calculating A:

We outset with A. We can encounter the following triangle:

enter image description here

The long side of the triangle is our radius R. The other side has length A/2 and we exercise not care nearly the third side. The angle in the right-most corner is Z/two (with Z = 36° being the bending nosotros calculated in the previous section). The angle S is a right-angle, so S = ninety°. We can calculate the third angle T because we know that the inner angles of a triangle sum up to 180°. Therefore, 180° = Due south + Z/ii + T. Solving for T, we get T = 180° - S - Z/2 = 180° - 90° - 36°/2 = 72°.

Next, nosotros utilise trigonometry to calculate A/2. Trigonometry teaches united states that A/2 = R * sin(T). Putting in the formula for T, we get A/2 = R * sin(72°). Solving for A, we become A = 2*R*sin(72°).

If you lot pick some value for R, for example R = 100, you can now calculate A with this formula. You would need a calculator for sin(72°), because it would be extremely difficult to calculate this in your head. Putting sin(72) into my calculator gives me 0.951056516. So for our choice R = 100, we know that A = 2 * R * sin(72°) = 2 * 100 * 0.951056516 = 190.211303259.

Computing B:

We apply the aforementioned technique to discover a formula for B. We run across the following triangle:

enter image description here

So the bottom side is the length of our radius R. The right side is B/2. We do non intendance about the tertiary side. The correct-about bending is three times Z/2. The angle Due south is a right-bending, so nosotros have Southward = xc°. We tin summate the remaining angle T with 180° = Due south + T + three*Z/2. Solving for T, we get T = 180° - Southward - three*Z/2 = 180° - 90° - 3*36°/2 = 36°. Ok then T = Z, we could accept too seen this from the moving picture, but now we have calculated it anyways.

Using trigonometry, we know that B/ii = R * sin(T), so we become the formula B = 2 * R * sin(36°) to calculate B for some pick of R.

Calculating C:

We see the following triangle:

enter image description here

So the lesser side has length A/2 and the height side has length B. We already have formulas for both of these sides. The third side is C, for which we want to notice a formula. The right-most angle is Z. The angle S is a right-bending, and then S = 90°. The top-nearly angle is three times Z/2.

Using trigonometry, we get C = sin(Z) * B.

Calculating D:

Nosotros see the following triangle:

enter image description here

Nosotros already take a formula for C. We desire to discover a formula for D. The superlative-most bending is Z/two (I could not fit the text into the triangle). The bottom-left angle Southward is a correct-angle.

Using trigonometry, we know that D = tan(Z/2) * C. The tan function is similar to the sin from the previous formulas. You lot can again put it into your reckoner to compute the value, and then for Z = 36°, I tin can put tan(36/two) into my computer and information technology gives me 0.324919696.

Calculating East:

Ok this is like shooting fish in a barrel, E = two*D.

Halfway done already!

Calculating F:

This is like to A and B:

enter image description here

We want to detect a formula for F. The acme side has length F/2. The lesser side has the length of our radius R. The correct-most corner has angle Z. Due south is a right-angle. We can summate T = 180° - S - Z = 180° - 90° - Z = ninety° - Z.

Using trigonometry, nosotros get F/2 = R * sin(T). Putting in the formula for T gives us F/two = R*sin(90° - Z). Solving for F gives u.s. F = 2*R*sin(90°-Z).

Computing G:

We see the post-obit triangle:

enter image description here

The top side has length F, we already know a formula for information technology. The right side has length Thousand, we want to discover a formula for it. We exercise not care about the lesser side. The left-most corner has angle Z/two. The right-most corner has angle 2*Z. The bottom corner has angle S, which is a correct-angle, then Southward = 90°. It was non immediately obvious to me that the red line and the light-green line are perfectly perpendicular to each other so that S really is a right-angle, but you lot can verify this by using the formula for the inner angles of a triangle, which gives yous 180° = Z/2 + two*Z + S. Solving for Due south gives us S = 180° - Z/2 - ii*Z. Using Z = 36°, we get S = 180° - 36°/ii - 2* 36° = 90°.

Using trigonometry, nosotros get K = F * sin(Z/2).

Calculating H:

We see the post-obit triangle:

enter image description here

The right side has length G, nosotros already take formula for that. The lesser side has length H, nosotros desire to detect a formula for that. We do not care well-nigh the third side. The superlative corner has angle Z, the bottom-correct corner has bending S. We already know that S is a right-angle from the last section.

Using trigonometry, we get H = G * tan(Z).

Calculating I:

This is piece of cake, I is on the aforementioned line equally A. Nosotros tin can meet that A can exist divided into A = I + H + E + H + I. We can simplify this to A = two*I + 2*H + E. Solving for I gives us I = (A - 2*H - E)/2.

Computing J:

Again this is piece of cake, J is on the aforementioned line every bit F. Nosotros can run across that F can exist divided into F = Grand + J + G. We can simplify that to F = 2*Chiliad + J. Solving for J gives us J = F - 2*K.

Writing the Python program

We at present have formulas for all the lines we were interested in! We can at present put these into a Python plan to draw the film.

Python gives yous helper functions for computing sin and tan. They are contained in the math module. And so y'all would add import math to the top of your program, and then you can apply math.sin(...) and math.tan(...) in your plan. However, there is 1 problem: These Python functions do not utilise degrees to measure out angles. Instead they use a different unit called "radians". Fortunately, it is easy to catechumen between degrees and radians: In degrees a total circle is 360°. In radians, a full circle is two*pi, where pi is a special constant that is approximately 3.14159265359.... Therefore, we can catechumen an angle that is measured in degrees into an bending that is measured in radians, by dividing the angle by 360° then multiplying information technology past 2*pi. We can write the post-obit helper functions in Python:

          import math  def degree_to_radians(angle_in_degrees):   full_circle_in_degrees = 360   full_circle_in_radians = 2 * math.pi    angle_in_radians = angle_in_degrees / full_circle_in_degrees * full_circle_in_radians    render angle_in_radians  def sin_from_degrees(angle_in_degrees):   angle_in_radians = degree_to_radians(angle_in_degrees)   return math.sin(angle_in_radians)  def tan_from_degrees(angle_in_degrees):   angle_in_radians = degree_to_radians(angle_in_degrees)   return math.tan(angle_in_radians)                  

We can now use our functions sin_from_degrees and tan_from_degrees to compute sin and tan from angles measured in degrees.

Putting it all together:

          from turtle import *  import math  # Functions to summate sin and tan ###########################################  def degree_to_radians(angle_in_degrees):   full_circle_in_degrees = 360   full_circle_in_radians = 2 * math.pi    angle_in_radians = angle_in_degrees / full_circle_in_degrees * full_circle_in_radians    return angle_in_radians  def sin_from_degrees(angle_in_degrees):   angle_in_radians = degree_to_radians(angle_in_degrees)   render math.sin(angle_in_radians)  def tan_from_degrees(angle_in_degrees):   angle_in_radians = degree_to_radians(angle_in_degrees)   render math.tan(angle_in_radians)  # Functions to calculate the angles ############################################  def get_X():   num_corners = 5   return (num_corners-2)*180 / num_corners  def get_Y():   render (360 - 2*get_X()) / two  def get_Z():   return 180 - 2*get_Y()  # Functions to calculate the lengths ###########################################  def get_A(radius):   Z = get_Z()   return 2 * radius * sin_from_degrees(90 - Z/two)  def get_B(radius):   Z = get_Z()   return 2 * radius * sin_from_degrees(90 - 3*Z/two)  def get_C(radius):   Z = get_Z()   return sin_from_degrees(Z) * get_B(radius)  def get_D(radius):   Z = get_Z()   render tan_from_degrees(Z/two) * get_C(radius)  def get_E(radius):   return 2 * get_D(radius)  def get_F(radius):   Z = get_Z()   return 2 * radius * sin_from_degrees(90 - Z)  def get_G(radius):   Z = get_Z()   return get_F(radius) * sin_from_degrees(Z/2)  def get_H(radius):   Z = get_Z()   return get_G(radius) * tan_from_degrees(Z)  def get_I(radius):   A = get_A(radius)   Due east = get_E(radius)   H = get_H(radius)   render (A - Due east - two*H) / two  def get_J(radius):   F = get_F(radius)   G = get_G(radius)   render F - 2*G  # Functions to depict the stars ##################################################  def back_to_center():   penup()   goto(0, 0)   setheading(0)   pendown()  def draw_small_star(radius):   penup()   forward(radius)   pendown()    Z = get_Z()    left(180)   right(Z/ii)    E = get_E(radius)   H = get_H(radius)   I = get_I(radius)    for i in range(0,5):     penup()     forward(I)      pendown()     forward(H)      penup()     forward(E)      pendown()     forward(H)      penup()     frontwards(I)      left(180)     right(Z)    back_to_center()  def draw_green_star(radius):   pencolor('greenish')   draw_small_star(radius)  def draw_blue_star(radius):   pencolor('blue')    Z = get_Z()   left(Z)    draw_small_star(radius)  def draw_red_star(radius):   pencolor('red')    Z = get_Z()    penup()   forward(radius)   pendown()    left(180)   right(Z)    M = get_G(radius)   J = get_J(radius)    for i in range(0,10):     pendown()     forward(G)      penup()     forward(J)      pendown()     forrard(G)      left(180)     right(two*Z)    back_to_center()  def draw_shape(radius):   draw_green_star(radius)   draw_blue_star(radius)   draw_red_star(radius)  radius = 400 draw_shape(radius) done()                  

Output:

enter image description here

Source: https://stackoverflow.com/questions/61820846/how-do-i-draw-this-shape-in-turtle

Posted by: maringois1977.blogspot.com

0 Response to "How To Draw A Rotating Pentagon In Python Turtle"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel