0
$\begingroup$

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)

$\endgroup$
6
  • $\begingroup$ "the constraints are $x_1^2+y_1^2+z_1^2=0$..." and these are real numbers? The only such real numbers satisfying this are for them all to be zero. Surely you mean to have some different constraints here $\endgroup$
    – JMoravitz
    Feb 23 at 2:23
  • $\begingroup$ @JMoravitz: Sorry, typo corrected. Meant 1 not 0. $\endgroup$ Feb 23 at 2:24
  • $\begingroup$ Now... as for finding a vector perpendicular to another... consider using the Gram-Schmidt process. Alternatively, take the cross product of your vector by an arbitrary other vector and normalize it after the fact. $\endgroup$
    – JMoravitz
    Feb 23 at 2:28
  • $\begingroup$ @JMoravitz: That won't work if $v_1$ is collinear with the arbitrary vector, it will result in the zero vector. $\endgroup$ Feb 23 at 2:34
  • 1
    $\begingroup$ "arbitrary" is not "random." When picking an arbitrary vector you can arbitrarily pick it such that it is not colinear. $\endgroup$
    – JMoravitz
    Feb 23 at 2:35
1
$\begingroup$

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}$$

$\endgroup$
3
  • $\begingroup$ It would be nice if there was a solution without a branch, but it looks like there isn't one. I find this somehow suprising. $\endgroup$ Feb 24 at 3:11
  • $\begingroup$ @AndrewTomazos: The fact that a branch/selection among candidates is needed here, has to do with avoiding singularities (and numerically, the unstable regions near singularities). In 2D, a single unit vector already defines the orientation, so there is nothing to choose, and no singularities. In 3D, we have three degrees of freedom, and any permutation (including partial negation) of the original vector leaves at least one singularity. For example, $(y, -z, x)$ is parallel to $(x, y, z)$ if $x = y z / (y + z)$; and $(z, -x, y)$ is parallel to $(x, y, z)$ if $x = y z / (y - z)$. $\endgroup$
    – Glärbo
    Feb 24 at 7:32
  • $\begingroup$ As to exactly why those singularities do exist, I do not know. $\endgroup$
    – Glärbo
    Feb 24 at 7:32
0
$\begingroup$

Pick any vector ${\bf v}_2 \neq {\bf v}_1$, then compute ${\bf v}_1 \times {\bf v}_2$, and normalize the result.

(Oh... I just saw @JMoravitz stated this in a comment. Upvote his comment, not my "answer.")

$\endgroup$
3
  • $\begingroup$ It seems strange but its actually difficult to pick a vector that is not collinear (or close to collinear) to $v_1$ in a generic and efficient fashion. I suppose I could find the maximum of $x_1, y_1, z_1$ and if x is the largest use $(0,1,1)$, if y is the largest use $(1,0,1)$ and if z is the largest use $(1,1,0)$ $\endgroup$ Feb 23 at 2:59
  • $\begingroup$ Who needs "generic"? Try this: ${\bf v}_2 = {\bf v}_1 + (1,0,0)^t$ (so long as ${\bf v}_1 \neq (1,0,0)^t$) and be done with it. ANY vector other than ${\bf v}_1$ will work. What could possibly be more "efficient" than that?! What does "close to collinear" have to do with anything? $\endgroup$ Feb 23 at 3:16
  • $\begingroup$ When two vectors are close to collinear it can cause numeric problems with the cross product and/or normalization. $\endgroup$ Feb 23 at 8:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.