# Closest to Another Vector Within a Given Angle

Given:

• 3 component vectors: $\vec x$ and $\vec y$
• Angle $\theta$
• The angle between $\vec x$ and $\vec y$ is greater than $\theta$

Find a 3 component vector $\vec z$ such that $\vec z$ is in the plane defined by $\vec x$ and $\vec y$ between $\vec x$ and $\vec y$, but the angle between $\vec x$ and $\vec z$ is exactly $\theta$.

If it matters the magnitude of $\vec z$ should be equal to the magnitude of the smaller of $\vec x$ and $\vec y$.

My question is: What's the equation that I'd use to find $\vec z$?

Component-wise calculations don't really make sense, cause I can't get the angle between single components, but this doesn't seem like a matrix question...

Let $\vec{z}=a\vec{x}+b\vec{y}$ such a vector, we must have \begin{align*} \vec{z}\cdot\vec{x}&=\|\vec{z}\|\|\vec{x}\|\cos \theta\\ a\vec{x}\cdot\vec{x}+b\vec{y}\cdot\vec{x}&=\|\vec{z}\|\|\vec{x}\|\cos\theta...(1) \end{align*} Let $\alpha$ the angle between $\vec{x}$ and $\vec{y}$, we also need \begin{align*} \vec{z}\cdot\vec{y}&= \|\vec{z}\|\|\vec{y}\|\cos(\alpha-\theta)\\ a\vec{x}\cdot\vec{y}+b\vec{y}\cdot\vec{y}&=\|\vec{z}\|\|\vec{y}\|\cos(\alpha-\theta)...(2) \end{align*} Then, we need solve for $a$ and $b$ equations $(1)$ and $(2)$. If also $\|\vec{z}\|=\|\vec{x}\|$ is needed we get the system of two linear equations \begin{align*} (\vec{x}\cdot\vec{x})\,\color{blue}{a}+(\vec{y}\cdot\vec{x})\,\color{blue}{b}&=\|\vec{x}\|^2\cos\theta\\ (\vec{x}\cdot\vec{y})\,\color{blue}{a}+(\vec{y}\cdot\vec{y})\,\color{blue}{b}&=\|\vec{x}\|\|\vec{y}\|\cos(\alpha-\theta) \end{align*}

• I understand that you distributed $\vec{v}$ to get a left hand side of: $a\vec{x}\cdot\vec{x}+b\vec{y}\cdot\vec{x}$ I don't get how you eliminated $\|\vec{v}\|$ on the right hand side: $\|\vec{x}\|^2\cos\theta$ – Jonathan Mee May 20 '15 at 16:34
• I am using $\mathbf{v}=\mathbf{z}$, hence I take $\|\mathbf{v}\|=\|\mathbf{z}\|=\|\mathbf{x}\|$ – Ángel Mario Gallegos May 20 '15 at 19:59
• I've gone ahead to accept your answer, as it is the correct solution, I've put an explanation of my understanding below. If you have a moment to go through and certify it that would be very much appreciated. – Jonathan Mee May 21 '15 at 14:43

This is an explanation of MarioG's answer which is the best way to find this.

The goal is to find magnitudes $a$ and $b$ such that: $a\vec{x} + b\vec{y} = \vec{z}$

We'll need to find $\phi$ where the angle between $\vec{x}$ and $\vec{y}$ is $\theta + \phi$. Since $\vec{x}$ and $\vec{y}$ are known we can use the law of cosines to find the angle between them: http://reference.wolfram.com/language/ref/VectorAngle.html

Now we can use the system of equations to solve for $a$ and $b$:

$$\left\{\begin{array}{l}\|\vec{x}\| = \|\vec{z}\|\\ a\vec{x} + b\vec{y} = \vec{z}\\ \vec{x} \cdot \vec{z} = \|\vec{x}\|\|\vec{z}\|\cos\theta\\ \vec{y} \cdot \vec{z} = \|\vec{y}\|\|\vec{z}\|\cos\phi\end{array}\right.$$

Simplifying out $\vec{z}$ this becomes:

$$\left\{\begin{array}{l}a\vec{x} \cdot \vec{x} + b\vec{x} \cdot \vec{y} = \|\vec{x}\|^2\cos\theta\\ a\vec{x} \cdot \vec{y} + b\vec{y} \cdot \vec{y} = \|\vec{x}\|\|\vec{y}\|\cos\phi\end{array}\right.$$

Now we'll define some constants to make simplification bearable:

• $k_1 = \vec{x} \cdot \vec{x}$
• $k_2 = \vec{x} \cdot \vec{y}$
• $k_3 = \|\vec{x}\|^2\cos\theta$
• $k_4 = \vec{y} \cdot \vec{y}$
• $k_5 = \|\vec{x}\|\|\vec{y}\|\cos\phi$

So our system of equations using these constants is:

$$\left\{\begin{array}{l}ak_1 + bk_2 = k_3\\ ak_2 + bk_4 = k_5\end{array}\right.$$

Solving for $a$ and $b$ our system becomes:

$$\left\{\begin{array}{l}a = \frac{k_3 - bk_2}{k_1}\\ b = \frac{k_5 - ak_2}{k_4}\end{array}\right.$$

We'll substitute for $b$ to solve for $a$:

1. $$a = \frac{k_3 - k_2\left(\frac{k_5 - ak_2}{k_4}\right)}{k_1}$$
2. $$a = \frac{\frac{k_3k_4 - k_2k_5 + ak_2k_2}{k_4}}{k_1}$$
3. $$a = \frac{k_3k_4 - k_2k_5 + ak_2k_2}{k_1k_4}$$
4. $$a = \frac{k_3k_4 - k_2k_5}{k_1k_4} + a\left(\frac{k_2k_2}{k_1k_4}\right)$$
5. $$a - a\left(\frac{k_2k_2}{k_1k_4}\right) = \frac{k_3k_4 - k_2k_5}{k_1k_4}$$
6. $$a\left(1 - \frac{k_2k_2}{k_1k_4}\right) = \frac{k_3k_4 - k_2k_5}{k_1k_4}$$
7. $$a\left(\frac{k_1k_4 - k_2k_2}{k_1k_4}\right) = \frac{k_3k_4 - k_2k_5}{k_1k_4}$$
8. $$a(k_1k_4 - k_2k_2) = k_3k_4 - k_2k_5$$
9. $$a = \frac{k_3k_4 - k_2k_5}{k_1k_4 - k_2k_2}$$

Plugging this back in to the equation that was solved for $b$ we get:

$$b = \frac{k_5 - k_2\left(\frac{k_3k_4 - k_2k_5}{k_1k_4 - k_2k_2}\right)}{k_4}$$

Removing the constant simplification:

$$a = \frac{\vec{y} \cdot \vec{y}\|\vec{x}\|^2\cos\theta - \vec{x} \cdot \vec{y}\|\vec{x}\|\|\vec{y}\|\cos\phi}{(\vec{x} \cdot \vec{x})(\vec{y} \cdot \vec{y}) - (\vec{x} \cdot \vec{y})^2}$$

$$b = \frac{\|\vec{x}\|\|\vec{y}\|\cos\phi - \vec{x} \cdot \vec{y}\left(\frac{\vec{y} \cdot \vec{y}\|\vec{x}\|^2\cos\theta - \vec{x} \cdot \vec{y}\|\vec{x}\|\|\vec{y}\|\cos\phi}{(\vec{x} \cdot \vec{x})(\vec{y} \cdot \vec{y}) - (\vec{x} \cdot \vec{y})^2}\right)}{\vec{y} \cdot \vec{y}}$$

Now an amazing simplification would be to normalize $\vec x$ and $\vec y$ before calculation. In which case, $\|\vec x\|$, $\|\vec y\|$, $\vec x \cdot \vec x$, and $\vec y \cdot \vec y$ are all 1. Which simplifies the above equations to:

$$a = \frac{\cos \theta - \vec x \cdot \vec y \cos \phi}{1 - (\vec x \cdot \vec y)^2}$$

$$b = \cos \phi - \vec x \cdot \vec y\left(\frac{\cos \theta - \vec x \cdot \vec y \cos \phi}{1 - (\vec x \cdot \vec y)^2}\right)$$

$b$ can be further simplified by distributing $-\vec x \cdot \vec y$ and moving $\cos \phi$ into the numerator:

$$b = \frac{\cos \phi - \vec x \cdot \vec y \cos \theta}{1 - (\vec x \cdot \vec y)^2}$$

$$\vec z = \frac{\vec x(\cos \theta - \vec x \cdot \vec y \cos \phi) + \vec y(\cos \phi - \vec x \cdot \vec y \cos \theta)}{1 - (\vec x \cdot \vec y)^2}$$