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};
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.
The same in python (with output in degrees):
Output:
angle: 90.0
elapsed in: 8.392333984375e-05
@Roger algorithm in swift
In pseudo-code, the vector BA (call it v1) is:
Similarly the vector BC (call it v2) is:
The dot product of
v1
andv2
is a function of the cosine of the angle between them (it's scaled by the product of their magnitudes). So first normalizev1
andv2
:Then calculate the dot product:
And finally, recover the angle: