计算三角形的角度Python
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18583214/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Calculate angle of triangle Python
提问by Shannon Hochkins
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:
我试图在下面找出三角形的角度,我知道它应该是 90 度,但是我不知道如何在下面实际计算它:
Here's what I've tried:
这是我尝试过的:
angle = math.cos(7/9.899)
angleToDegrees = math.degrees(angle)
returns: 43.XX
What am I doing wrong?
我究竟做错了什么?
采纳答案by John La Rooy
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.
这精确到 4 位有效数字。如果您提供更精确的 C 值,您将获得更准确的结果。
>>> C=9.899494936611665
>>> degrees(acos((A * A + B * B - C * C)/(2.0 * A * B)))
90.0
回答by yoshi
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.
我认为您正在寻找 math.acos 而不是 math.cos,您想返回其值为这两条边之比的角度。不取其余弦。
回答by colcarroll
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).
三角函数将角度转换为某个三角形某条腿的长度。特别地,切线是对边与相邻边的比值。 math.tan(7/7)
是对角为 1(=7/7) 弧度的直角三角形的长度。这个长度 (~1.557) 恰好接近 90 度 (pi/2 ~ 1.571) 的弧度数。
As noted, you're looking for an inverse trig function to convert a length back into an angle.
如前所述,您正在寻找一个反三角函数来将长度转换回角度。
回答by HasanShovon
use this:
用这个:
import math
AB = float(input())
BC = float(input())
print(str(int(round(math.degrees(math.atan2(AB, BC)))))+'°')
回答by himanshu
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.
这接受两个输入作为三角形的两个高度,您可以以适当的度数格式获得输出角度。