To start, I would like to apologize if the answer to my question was easily googled, I am quite new to this and googling "Find closest vector to A which is perpendicular to B" gave me no results.

My problem:

I am a procedural generation programmer looking for a way to do something slightly similar to a cross product:

The cross product returns a vector which is perpendicular to two other vectors.

I need a vector which is only perpendicular to one vector. However, I need this vector to be the closest vector to another vector.

In other words, I would like to find a Vector C, with the smallest amount of difference between its self and Vector A -- But this vector MUST be perpendicular to Vector B.

Is there any way to do this using a series of Dot/Cross products (or some other sort of vector arithmetic)?

I am, sadly, not familiar with linear algebra, so I won't be able to solve for C in an answer in the form of an equation, so if you do decide to post an equation, please also post how I might make a computer solve for C using that equation.

Extra details:
I am working with unit vectors (Magnitude = 1)
I am working in 3D (not 2D... or 4D for that matter)
By "Closest Vector to A" I mean "Dot product between C and A closest to 1"

  • $\begingroup$ Can you quantify what you mean by "closest"? $\endgroup$ – Muphrid Jun 3 '13 at 22:10
  • $\begingroup$ Dot product between C and A closest to 1 $\endgroup$ – Georges Oates Larsen Jun 3 '13 at 22:11
  • $\begingroup$ Do you basically mean the smallest 2 norm of the difference between the two vectors? $\endgroup$ – Hunle Oct 30 '17 at 0:27

You can do this with elementary vector algebra. Call $D = A \times B$, and then $C = B \times D$. $C$ is automatically orthogonal to $B$.

Of course, it's a little difficult to know that this is indeed the vector most like $A$. I reasoned this out using geometric algebra: there is a unique plane denoted $iB$ that is orthogonal to $B$ (and thus contains all vectors orthogonal to $B$). The vector in $iB$ closest to $A$ is just the projection of $A$ onto this subspace. This projection is denoted $[A \cdot (iB)](iB)^{-1}$, and this is equivalent to the prescription I have given using the cross product above. Geometric algebra is ideally suited to formulating problems like these, as it naturally lets you work with orthogonal planes and relationships between vectors and planes.

  • $\begingroup$ I see, and by X, you mean cross product, yes? $\endgroup$ – Georges Oates Larsen Jun 3 '13 at 22:29
  • 1
    $\begingroup$ Ah yes, if I pretend X is cross product, your answer makes sense :D $\endgroup$ – Georges Oates Larsen Jun 3 '13 at 22:32

I believe I have to offer an even more intuitive solution.

You have a vector A which is sadly not perpendicular to vector B. The two vectors span a plane, and no combination of them would point out of this plane. We can now modify A to get rid of any parts within it that are pointing into the same direction as B does to build a vector C that is perpendicular to B.
$$C = A - B (B\cdot A)$$ The dot represents the dot product and $B\cdot A$ will be $0$ if the vectors are already orthogonal and $1$ if they are pointing into the same direction. The scalar product is defined by $B\cdot A = |B|*|A|*cos(\alpha)$ with $\alpha$ being the angle between $A$ and $B$.

To argue about whether the proposed $C$ is actually the closest to $A$, we ignore the absolute values of the vectors for now, because they only scale the result; they don't affect the direction of $C$.
Remember, we are still within the plane spanned by $A$ and $B$, so we cannot be off the direction of $A$ in the third dimension. In the plane, there are only two options for a perpendicular direction: Either the desired one or the exact opposite direction.
The cosine and thus also the dot product are positive for $\alpha$ between $0°$ and $90°$, so in that case we get what we want. If $\alpha$ is larger, we want the direction of $B$ to be subtracted instead of added (as long as $\alpha<270$ ) - that is what happens. Similarly for an even larger angle, the cosine becomes positive again.
Now what about the length of that vector $C$? You can rescale it however you like, e.g. to the length of $A$.

Wikipedia features some additional intuitive insight:

The scalar projection (or scalar component) of a Euclidean vector a in the direction of a Euclidean vector b is given by ${\displaystyle a_{b}=\left\|\mathbf {a} \right\|\cos \theta ,}$ where θ is the angle between a and b.

In terms of the geometric definition of the dot product, this can be rewritten

${\displaystyle a_{b}=\mathbf {a} \cdot {\widehat {\mathbf {b} }},}$

where ${\displaystyle {\widehat {\mathbf {b} }}=\mathbf {b} /\left\|\mathbf {b} \right\|}$ is the unit vector in the direction of b.

The dot product is thus characterized geometrically by

${\displaystyle \mathbf {a} \cdot \mathbf {b} =a_{b}\left\|\mathbf {b} \right\|=b_{a}\left\|\mathbf {a} \right\|.}$

This means that if you want to have $C$ the size of its projection, as Muphrid suggested in their Answer, you can simply divide $C$ (from above) by $|B|$. If $B$ has a magnitued of $1$, you can skip that step.


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.