3d 空间中 3 个点之间的角度 [英] Angle between 3 points in 3d space
本文介绍了3d 空间中 3 个点之间的角度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有 3 个包含 X、Y、Z 坐标的点:
I have 3 points containing X, Y, Z coordinates:
var A = {x: 100, y: 100, z: 80},
B = {x: 100, y: 175, z: 80},
C = {x: 100, y: 100, z: 120};
坐标是来自 3d CSS 变换的像素.如何获得向量 BA 和 BC 之间的角度?一个数学公式就行了,JavaScript 代码会更好.谢谢.
The coordinates are pixels from a 3d CSS transform. How can I get the angle between vectors BA and BC? A math formula will do, JavaScript code will be better. Thank you.
推荐答案
在伪代码中,向量 BA(称为 v1)是:
In pseudo-code, the vector BA (call it v1) is:
v1 = {A.x - B.x, A.y - B.y, A.z - B.z}
同样,向量 BC(称为 v2)是:
Similarly the vector BC (call it v2) is:
v2 = {C.x - B.x, C.y - B.y, C.z - B.z}
v1
和 v2
的点积是它们之间夹角余弦的函数(按它们的大小乘积缩放).所以首先规范化v1
和v2
:
The dot product of v1
and v2
is a function of the cosine of the angle between them (it's scaled by the product of their magnitudes). So first normalize v1
and v2
:
v1mag = sqrt(v1.x * v1.x + v1.y * v1.y + v1.z * v1.z)
v1norm = {v1.x / v1mag, v1.y / v1mag, v1.z / v1mag}
v2mag = sqrt(v2.x * v2.x + v2.y * v2.y + v2.z * v2.z)
v2norm = {v2.x / v2mag, v2.y / v2mag, v2.z / v2mag}
然后计算点积:
res = v1norm.x * v2norm.x + v1norm.y * v2norm.y + v1norm.z * v2norm.z
最后,恢复角度:
angle = acos(res)
这篇关于3d 空间中 3 个点之间的角度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文