Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calculate angle of triangle Python

I'm trying to find out the angle of the triangle in the following, I know it should be 90 degrees, however I don't know how to actually calculate it in the following:

enter image description here

Here's what I've tried:

angle = math.cos(7/9.899)
angleToDegrees = math.degrees(angle)

returns: 43.XX

What am I doing wrong?

like image 875
Shannon Hochkins Avatar asked Sep 03 '13 02:09

Shannon Hochkins


People also ask

What is the formula for triangle in Python?

s = (a + b + c) / 2. # calculate the area. area = (s*(s-a)*(s-b)*(s-c)) ** 0.5. print('The area of the triangle is %0.2f' %area)


2 Answers

It's a little more compicated than that. You need to use the law of cosines

>>> A = 7
>>> B = 7
>>> C = 9.899
>>> from math import acos, degrees
>>> degrees(acos((A * A + B * B - C * C)/(2.0 * A * B)))
89.99594878743945

This is accurate to 4 significant figures. If you provide a more precise value of C, you get a more accurate result.

>>> C=9.899494936611665
>>> degrees(acos((A * A + B * B - C * C)/(2.0 * A * B)))
90.0
like image 99
John La Rooy Avatar answered Oct 14 '22 16:10

John La Rooy


You can use this also.

print(str(int(round(math.degrees(math.atan2(x,y)))))+'°')

This accepts two inputs as two heights of the triangle and you can get the output angle in proper degree format.

like image 34
himanshu Avatar answered Oct 14 '22 16:10

himanshu