Calculate angle of triangle Python

我只是一个虾纸丫 提交于 2020-03-01 03:21:07

问题


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:

Here's what I've tried:

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

returns: 43.XX

What am I doing wrong?


回答1:


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



回答2:


i think you're looking for math.acos not math.cos, you want to return the angle whose value is the ratio of those two sides. not take its cosine.




回答3:


Trig functions will convert an angle into a length of a certain leg of a certain triangle. In particular, the tangent is the ratio of the opposite side to the adjacent side. math.tan(7/7) is the length of the right triangle opposite an angle of 1(=7/7) radian. This length (~1.557) just happens to be near to the number of radians which is 90 degrees (pi/2 ~ 1.571).

As noted, you're looking for an inverse trig function to convert a length back into an angle.




回答4:


use this:

import math
AB = float(input())
BC = float(input())

print(str(int(round(math.degrees(math.atan2(AB, BC)))))+'°')



回答5:


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.



来源:https://stackoverflow.com/questions/18583214/calculate-angle-of-triangle-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!