Reputation: 2416
I have a vector1 which I know the 3D coordinates (vector1 can be in any direction) and I would like to find two perpendicular vectors to this vector1 (the two perpendicular vectors must be perpendicular to each other too).
What is the fastest way to find the two vectors programmatically(in Java if possible)?
I tried to rotate the vector1 by 90 degrees but it doesn't seems to always work depending of the direction of vector1.
Edit: Perpendicular vectors can be in any directions.
Upvotes: 3
Views: 1492
Reputation: 18566
To find the first vector, you can apply the following algorithm:
Let's assume that the original vector is (A, B, C)
. Two vectors are perpendicular if their scalar product is 0. So we get an equation A * x + B * y + C * z = 0
. At least one of the A
, B
or C
is not zero. Let's assume that C
is not zero. Then a vector(1
, 1
, -(A + B) / C
) fits. The case when C = 0
but A != 0
or B != 0
can be handled in a similar way.
Finding the second vector is much easier: you can use a vector product of the original vector and the first vector. That's it.
Upvotes: 6