luc*_*uct 37 python algorithm math
我有一个三角形(A,B,C),我试图找到三个点中每对之间的角度.
问题是我可以在网上找到的算法用于确定向量之间的角度.使用向量,我将计算从(0,0)到我所拥有的点的向量之间的角度,并且不会给出三角形内的角度.
好的,这是在维基百科页面上的方法之后和减去值之后的Python中的一些代码:
import numpy as np
points = np.array([[343.8998, 168.1526], [351.2377, 173.7503], [353.531, 182.72]])
A = points[2] - points[0]
B = points[1] - points[0]
C = points[2] - points[1]
for e1, e2 in ((A, B), (A, C), (B, C)):
num = np.dot(e1, e2)
denom = np.linalg.norm(e1) * np.linalg.norm(e2)
print np.arccos(num/denom) * 180
Run Code Online (Sandbox Code Playgroud)
这给了我60.2912487814,60.0951900475和120.386438829,所以我做错了什么?
Jas*_*n S 37
这里有两个错误.
从弧度转换为度数时,你错过了π因子(它是×180 /π)
你必须小心矢量的符号,因为它们是有向线段.
如果我进行这些修改,我会得到一个有意义的结果:
import numpy as np
points = np.array([[343.8998, 168.1526], [351.2377, 173.7503], [353.531, 182.72]])
A = points[2] - points[0]
B = points[1] - points[0]
C = points[2] - points[1]
angles = []
for e1, e2 in ((A, B), (A, C), (B, -C)):
num = np.dot(e1, e2)
denom = np.linalg.norm(e1) * np.linalg.norm(e2)
angles.append(np.arccos(num/denom) * 180 / np.pi)
print angles
print sum(angles)
Run Code Online (Sandbox Code Playgroud)
打印出来的
[19.191300537488704, 19.12889310421054, 141.67980635830079]
180.0
Run Code Online (Sandbox Code Playgroud)
我可能会使事情更加对称,并使用循环的A,B,C向量并求和为零:
import numpy as np
points = np.array([[343.8998, 168.1526], [351.2377, 173.7503], [353.531, 182.72]])
A = points[1] - points[0]
B = points[2] - points[1]
C = points[0] - points[2]
angles = []
for e1, e2 in ((A, -B), (B, -C), (C, -A)):
num = np.dot(e1, e2)
denom = np.linalg.norm(e1) * np.linalg.norm(e2)
angles.append(np.arccos(num/denom) * 180 / np.pi)
print angles
print sum(angles)
Run Code Online (Sandbox Code Playgroud)
打印出来的
[141.67980635830079, 19.12889310421054, 19.191300537488704]
180.0
Run Code Online (Sandbox Code Playgroud)
点积中的减号是因为我们试图获得内角.
对不起,我们在你需要的时候通过关闭这个问题把你赶走了.