Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Computing two vectors that are perpendicular to third vector in 3D

Tags:

math

vector

3d

What is the best (fastest) way to compute two vectors that are perpendicular to the third vector(X) and also perpendicular to each other?

This is how am I computing this vectors right now:

// HELPER - unit vector that is NOT parallel to X
x_axis = normalize(X);
y_axis = crossProduct(x_axis, HELPER);
z_axis = crossProduct(x_axis, y_axis);

I know there is infinite number of solutions to this, and I don't care which one will be my solution.

What is behind this question: I need to construct transformation matrix, where I know which direction should X axis (first column in matrix) be pointing. I need to calculate Y and Z axis (second and third column). As we know, all axes must be perpendicular to each other.

like image 880
sidon Avatar asked May 22 '12 12:05

sidon


People also ask

How do you find a vector perpendicular to another vector in 3d?

To construct a vector that is perpendicular to another given vector, you can use techniques based on the dot-product and cross-product of vectors. The dot-product of the vectors A = (a1, a2, a3) and B = (b1, b2, b3) is equal to the sum of the products of the corresponding components: Aβˆ™B = a1_b2 + a2_b2 + a3_b3.

How do you prove that 2 3d vectors are perpendicular?

The vectors ⃑ 𝐴 and ⃑ 𝐡 are perpendicular if, and only if, their dot product is equal to zero: ⃑ 𝐴 β‹… ⃑ 𝐡 = 0 .

How do you find a vector perpendicular to two given vectors?

Explanation: Cross product of vectors A and B is perpendicular to each vector A and B. ∴ for two vectors β†’Aandβ†’B if β†’C is the vector perpendicular to both. =(A2B3βˆ’B2A3)Λ†iβˆ’(A1B3βˆ’B1A3)Λ†j+(A1B2βˆ’B1A2)Λ†k .

How do you find a vector parallel to another vector in 3d?

How Do You Find a Vector Parallel to a Given Vector? To find a vector that is parallel to a given vector a, just multiply it by any scalar.


2 Answers

What I have done, provided that X<>0 or Y<>0 is

  1. A = [-Y, X, 0]
  2. B = [-X*Z, -Y*Z, X*X+Y*Y]

and then normalize the vectors.

[ X,Y,Z]Β·[-Y,X,0] = -X*Y+Y*X = 0
[ X,Y,Z]Β·[-X*Z,-Y*Z,X*X+Y*Y] = -X*X*Z-Y*Y*Z+Z*(X*X+Y*Y) = 0
[-Y,X,0]Β·[-X*Z,-Y*Z,X*X+Y*Y] = Y*X*Z+X*Y*Z = 0

This is called the nullspace of your vector.

If X=0 and Y=0 then A=[1,0,0], B=[0,1,0].

like image 96
John Alexiou Avatar answered Oct 19 '22 04:10

John Alexiou


This is the way to do it.
It's also probably the only way to do it. Any other way would be mathematically equivalent.
It may be possible to save a few cycles by opening the crossProduct computation and making sure you're not doing the same multiplications more than once but that's really far into micro-optimization land.

One thing you should be careful is of course the HELPER vector. Not only does it has to be not parallel to X but it's also a good idea that it would be VERY not parallel to X. If X and HELPER are going to be even somewhat parallel, your floating point calculation is going to be unstable and inaccurate. You can test and see what happens if the dot product of X and HELPER is something like 0.9999.

like image 31
shoosh Avatar answered Oct 19 '22 02:10

shoosh