Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angle between 3 points in 3d space

Tags:

3d

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.

enter image description here

like image 551
Mircea Avatar asked Nov 01 '13 15:11

Mircea


People also ask

How do you find the angle between three points in 3D space?

To calculate the angle between two vectors in a 3D space: Find the dot product of the vectors. Divide the dot product by the magnitude of the first vector. Divide the resultant by the magnitude of the second vector.

What is a 3 dimensional angle?

-d angle means the angle formed by the lines in three-dimensional space. Using the vectors in -d, we can calculate the -d angles. Formula: The -d angle between two vectors and is, c o s θ = A → · B → | A → | | B → | , where and are the magnitudes of the vectors. Hence,3-d angle is explained.


1 Answers

In pseudo-code, the vector BA (call it v1) is:

v1 = {A.x - B.x, A.y - B.y, A.z - B.z}

Similarly the vector BC (call it v2) is:

v2 = {C.x - B.x, C.y - B.y, C.z - B.z}

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)
like image 80
Roger Rowland Avatar answered Oct 18 '22 19:10

Roger Rowland