Vector perpendicular to 3d vector

algorithmseuclidean-geometrygeometrylinear algebravector-spaces

Given a unit vector $v_1 \in \mathbb{R}^3 : |v_1| = 1$, let $v_1 = (x_1,y_1,z_1)$

What's the simplest way to find another unit vector $v_2 = (x_2, y_2, z_2)$ such that they are perpendicular (ie $v_1 \cdotp v_2 = 0$) ?

Notice the constraints are:

\begin{align}
x_1x_2 + y_1y_2 + z_1z_2 &= 0,\\
{x_1}^2 + {y_1}^2 + {z_1}^2 &= 1,\\
{x_2}^2 + {y_2}^2 + {z_2}^2 &= 1.
\end{align}

What is a solution for $x_2$, $y_2$, $z_2$ in terms of $x_1$, $y_1$, $z_1$ that is easy to express and calculate?

(Yes there is an infinite family of such vectors $v_2$ for a given $v_1$, I just want one, any one)

Best Answer

It looks like OP is looking for an algorithm to find a perpendicular vector.

Let $\mathbf{p}_1 = (x, y, z)$ be the original unit vector, $x^2 + y^2 + z^2 = 1$. I shall use notation $$\mathbf{p}_1 = (x, y, z) = \left[\begin{matrix}x\\y\\z\\ \end{matrix}\right]$$ where the parenthesised form $(x, y, z)$ is just shorthand for the proper vector/matrix form.

Construct two helper vectors by rotating $\mathbf{p}$ 90° around two different axes (the axes being perpendicular to each other), say around the $z$ axis and the $y$ axis, $$\begin{aligned} \mathbf{q}_1 &= (y ,\, -x ,\, z) \\ \mathbf{q}_2 &= (z ,\, y ,\, -x) \\ \end{aligned}$$ and calculate their vector cross products wrt. the original vector: $$\begin{aligned} \mathbf{d}_1 = \mathbf{p} \times \mathbf{q}_1 &= ( y z + x z ,\, y z - x z ,\, - y^2 - x^2 ) \\ \mathbf{d}_2 = \mathbf{p} \times \mathbf{q}_2 &= ( - y z - x y ,\, z^2 + x^2 ,\, x y - y z ) \\ \end{aligned}$$ One of these may be a zero vector (or very small), depending on how close the original $\mathbf{p}$ was to the respective rotation axis, but if nonzero, they are perpendicular to the original vector. So, pick the larger one in magnitude: $$\mathbf{p}_2 = \begin{cases} \displaystyle \frac{\mathbf{d}_1}{\left\lVert \mathbf{d}_1 \right\rVert}, & \left\lVert \mathbf{d}_1 \right\rVert \ge \left\lVert \mathbf{d}_2 \right\rVert \\ \displaystyle \frac{\mathbf{d}_2}{\left\lVert \mathbf{d}_2 \right\rVert}, & \left\lVert \mathbf{d}_1 \right\rVert \lt \left\lVert \mathbf{d}_2 \right\rVert \\ \end{cases}$$

Related Question