0
import numpy as np
import math as m
import turtle as t

s=[3, 4, 5]

#expression below is used to calculate the angle between two sides of a triangle
a1=m.degrees(np.arccos(((s[1]**2 + s[2]**2 - s[0]**2) / (2 * s[1] * s[2]))))
a2=m.degrees(np.arccos(((s[0]**2 + s[2]**2 - s[1]**2) / (2 * s[0] * s[2]))))
a3=m.degrees(np.arccos(((s[0]**2 + s[1]**2 - s[2]**2) / (2 * s[0] * s[1]))))

a=[a1, a2, a3]
print(a)

p=t.Turtle()

for i in range(len(a)):
    p.forward(s[i]*25)
    p.left(180-a[i])

t.done()

the angles calculated are correct and the turtle is running fine too, except it just doesn't connect at the end. i suspect maybe the angles and the sides must be in different orientation to make it work, but i can't find the correct orientation for that.

InSync
  • 4,851
  • 4
  • 8
  • 30

2 Answers2

1

Hmm, 3-4-5 is the famous proportion known since ancient Aegypt to build a right triangle. As you use the line sizes in that order, the first angle must be the right angle.

Just change a to:

a = [a3, a1, a2]

and your turtle will give you a nice right triangle.

Serge Ballesta
  • 143,923
  • 11
  • 122
  • 252
  • but that proportions were just for an example, in the actual code i'll be using a few random sides, any suugestions for that, for eg [7,8,9] what order should the angles be then ? – Parth Ratra Jun 07 '23 at 14:32
  • I could not understand your angle calculations... But I have just tested with 7, 8, 9, and some other values and the turtle correctly gave a nice closed triangle. It looks like the angles are to be used in that order... – Serge Ballesta Jun 07 '23 at 14:45
  • ok, i tried it and [largest angle, mid angle, smallest angle] with [smallest side, mid side, largest side] works just fine thanks a lot ! – Parth Ratra Jun 08 '23 at 10:46
0

For this to work for arbitrary triangles, your ordering of the angle equations can't be arbitrary, you need to do them in order. Rather than reordering the angles in the list, as @SergeBallesta reasonably does (+1), I would make sure your angle equations are also done in the same order as the sides:

# s0 -> s1 -> s2 -> s0

a1=m.degrees(np.arccos(((s[0]**2 + s[1]**2 - s[2]**2) / (2 * s[0] * s[1]))))
a2=m.degrees(np.arccos(((s[1]**2 + s[2]**2 - s[0]**2) / (2 * s[1] * s[2]))))
a3=m.degrees(np.arccos(((s[2]**2 + s[0]**2 - s[1]**2) / (2 * s[2] * s[0]))))

A complete rework follows below, including switching turtle to radians rather than switching all the angles to degrees:

import turtle
from math import pi
from numpy import arccos

sides = [3, 4, 5]

# Calculate the angles between sides of a triangle
a1 = arccos(((sides[0]**2 + sides[1]**2 - sides[2]**2) / (2 * sides[0] * sides[1])))
a2 = arccos(((sides[1]**2 + sides[2]**2 - sides[0]**2) / (2 * sides[1] * sides[2])))
a3 = arccos(((sides[2]**2 + sides[0]**2 - sides[1]**2) / (2 * sides[2] * sides[0])))

angles = [a1, a2, a3]

turtle.radians()

for side, angle in zip(sides, angles):
    turtle.forward(side * 25)
    turtle.left(pi - angle)

turtle.done()
cdlane
  • 40,441
  • 5
  • 32
  • 81