Linear Algebra – Find Closest Vector to A Perpendicular to B

cross productlinear algebra

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"

Best Answer

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.