Given an arbitrary vector, I can find any orthogonal vector by solving $ax + by + cz = 0$.

I want to find the "most upright" orthogonal (unit) vector, where $z$ is maximized. There must be a straightforward closed form solution to this, right?


Using the observation made by dtldarek in his answer, you’re looking for the unit vector in the plane generated by $w=(a,b,c)$ and $e_3=(0,0,1)$ that is perpendicular to $w$ and has a non-negative $z$-coordinate. Observe that such a vector is also perpendicular to $e_3\times w$, so the vector you seek is $w\times(e_3\times w)$, normalized.

  • $\begingroup$ Elegant! $\ddot\smile$ $\endgroup$ – dtldarek May 16 '17 at 19:56
  • $\begingroup$ Yup, I have to say, that was pretty darn simple, and so obvious in retrospect. This one will be tucked away in my toolkit forever after. Thank you! $\endgroup$ – David Parks May 16 '17 at 21:17
  • 1
    $\begingroup$ This one is actually used a bunch in the 3d graphics world as part of creating orthonormal matrices which represent the effects of looking in a direction. $\endgroup$ – Cort Ammon May 17 '17 at 0:41
  • $\begingroup$ @CortAmmon Yep. That’s what led me to look at the problem from this point of view, so to speak. $\endgroup$ – amd May 17 '17 at 0:43
  • $\begingroup$ This was exactly my use case, I needed to compute the point of gaze as the head rotates left/right while focused at an angle. This vector provides the axis of rotation which then allowed me to apply Rodrigues' rotation formula to perform the rotation. $\endgroup$ – David Parks May 17 '17 at 17:20

Restating your problem (and adding a condition), you are given a vector $\vec{v}=\langle a,b,c\rangle$ and you want to find a vector $\vec{w}=\langle x,y,z\rangle$ so that

  1. $\vec{w}\cdot\vec{v}=0$,
  2. $\|\vec{w}\|=1$
  3. $\vec{w}\cdot\vec{e}_3$ is maximized

As you state, you want to maximize $z$ subject to \begin{align*} ax+by+cz&=0\\ x^2+y^2+z^2&=1 \end{align*}

Perhaps a Lagrange multiplier approach would be good here: \begin{align*} 0&=\lambda a+2\mu x\\ 0&=\lambda b+2\mu y\\ 1&=\lambda c+2\mu z\\ 0&=ax+by+cz\\ 1&=x^2+y^2+z^2 \end{align*}

We should really assume that $a$ and $b$ are not both zero because otherwise $\vec{v}$ points vertically and there are no orthogonal, upward pointing vectors.

  • If $\mu=0$, then we get that $\lambda=\frac{1}{c}$ (and $c\not=0$), but then the first two equations are $0=\frac{a}{c}$ and $0=\frac{b}{c}$, so $a=0=b$ This contradicts our assumption, so we're ok here.

  • If $\mu\not=0$, then we can solve for $x$, $y$, and $z$: \begin{align*} x&=-\frac{\lambda a}{2\mu}\\ y&=-\frac{\lambda b}{2\mu}\\ z&=\frac{1-\lambda c}{2\mu} \end{align*} We can substitute these into the fourth equation (and cancel the $2\mu$'s) to get $$ -\lambda a^2-\lambda b^2-\lambda c^2+c=0. $$ Now, we make things simpler by adding the assumption that $\langle a,b,c\rangle $ is a unit vector, so we could rewrite this equation as $c=\lambda$. If not, we use $c=\lambda\|\vec{v}\|$ throughout the rest of this problem.

Therefore, \begin{align*} x&=-\frac{ac}{2\mu}\\ y&=-\frac{bc}{2\mu}\\ z&=\frac{1-c^2}{2\mu} \end{align*} Substituting all of this into the final equation gives $$ 4\mu^2=a^2c^2+b^2c^2+1-2c^2+c^4=(a^2+b^2+c^2)c^2+1-2c^2. $$ Therefore $$ 4\mu^2=1-c^2 $$ or $$ \mu=\pm\sqrt{\frac{1-c^2}{4}} $$ Substituting these into the formulae for $x,y,z$ gives that \begin{align*} x&=\mp\frac{ac}{\sqrt{1-c^2}}\\ y&=\mp\frac{bc}{\sqrt{1-c^2}}\\ z&=\pm\frac{1-c^2}{\sqrt{1-c^2}} \end{align*} Depending on the signs, you should get the largest and smallest that $z$ could be (provided I made no errors).

  • $\begingroup$ Ahhh, lagrange multipliers... there's still scar tissue in my brain from the last time I had to deal with them. I'm really happy to see a straightforward application that I can get my head around. This is useful beyond the particular problem I'm solving right now. Thank you! $\endgroup$ – David Parks May 16 '17 at 18:32
  • $\begingroup$ @amd Yes, I added that assumption, but it doesn't really matter since orthogonality is independent of length. So, if you wanted to drop it, you would have $c=\lambda\|v\|$, and carry this through the computation. $\endgroup$ – Michael Burr May 16 '17 at 19:55
  • $\begingroup$ @amd Agreed, I made a change to the text. $\endgroup$ – Michael Burr May 16 '17 at 20:54

Assuming $\mathbb{R}^3$, a geometric approach would be to observe that the solution would belong to plane generated by $(a,b,c)$ and $(0,0,1)$. That forces the ratio between $x$ and $y$ to be the same as between $a$ and $b$. In other words, we have two equations:


If $c = 0$, then $(0,0,z)$ is your solution for any $z>0$. Otherwise, if $x=ka$ and $y=kb$, then $k(a^2+b^2)+cz=0$ and $z=-k\frac{a^2+b^2}{c}$. To maximize $z$ make sure $z\geq 0$, that is, $k\cdot c \leq 0$.

I hope this helps $\ddot\smile$

  • $\begingroup$ Might be worth noting that the second equation in the system is that of the plane spanned by $(a,b,c)$ and the $z$-axis. $\endgroup$ – amd May 16 '17 at 22:14

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.