O'Niel
O'Niel

Reputation: 1787

Calculate angles triangle having three legs

I had two legs of a triangle given, after the third one is calculated using Pythagoras I had to find the angles of the triangle to be able to draw the triangle in Python using Turtle.

I have tried the cosinus-formula to find the angles, but it ain't working. I'm not getting the desired result(s).

Code:

import math
import turtle

#Legs
a = 70
b = 60

c_pwr = a**2 + b**2
c = math.sqrt(c_pwr)

print("Langste zijde is: ", c)

#Angles
A = math.acos((b**2 + c**2 - a**2) / (2 * b * c)) * 100
B = math.acos((c**2 + a**2 - b**2) / (2 * c * a)) * 100
C = 360 - A - B

print(A, " ", B, " ", C)

turtle.forward(a)
turtle.right(B)
turtle.forward(b)
turtle.right(A)
turtle.forward(c)

input()

What am I doing wrong and how to fix it? Thanks!

Upvotes: 2

Views: 1043

Answers (2)

eyllanesc
eyllanesc

Reputation: 244301

acos returns the side in radians so you must convert it to sexagesimal degrees, for this you must multiply by 180/π. We also know that the sum of internal angles is 180, so the third angle is 180-A-B.

Another question is the angle that must be passed to draw, by default drawing from right to left advancing to, then you must rotate 180-A, advance c, rotate 180-B and advance b

a = 70
b = 60

c_pwr = a**2 + b**2
c = math.sqrt(c_pwr)

print("Langste zijde is: ", c)

#Angles
A = math.acos((b**2 + c**2 - a**2) / (2 * b * c))*180/math.pi
B = math.acos((c**2 + a**2 - b**2) / (2 * c * a))*180/math.pi

C = 180 - A - B

print(A, " ", B, " ", C)

turtle.forward(a)
turtle.right(180-B)
turtle.forward(c)
turtle.right(180-A)
turtle.forward(b)

Output:

enter image description here

Upvotes: 3

SwiftsNamesake
SwiftsNamesake

Reputation: 1578

I'm not sure why you're not using the definition

A = asin(a/c)

(A being the angle opposite to a).

Nor do I understand why you're multiplying by 100. To convert from radians to degrees, you'd multiply by 180/π.

Lastly, the angles in a triangle sum to 180 rather than 360. Putting it together:

import math

# Legs
a = 70
b = 60
c = math.sqrt(a**2 + b**2)

# Angles
A = math.asin(a/c) * 180/math.pi
B = math.asin(b/c) * 180/math.pi
C = 180 - A - B

Upvotes: 3

Related Questions