在三维空间3点之间的角度角度、空间

2023-09-07 22:04:37 作者:兜兜有棒棒糖︶ε╰

我有一个包含X 3点,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};

的坐标是从一个三维的CSS变换像素。 我怎样才能获得向量BA和BC之间的角度? 数学公式会做,JavaScript的code会更好。 谢谢你。

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.

推荐答案

在伪code,矢量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}

然后计算的点积:

Then calculate the dot product:

res = v1norm.x * v2norm.x + v1norm.y * v2norm.y + v1norm.z * v2norm.z

最后,恢复的角度:

And finally, recover the angle:

angle = acos(res)