1
\$\begingroup\$

I've solved this using the law of sines, but I was wondering if there's a way to do this with just vector math.

Here's a diagram that will hopefully help illustrate what I'm trying to do:

enter image description here

Given vector a and the direction of b, how can I find the magnitude of b? Both vectors share a common origin point. The white line is just supposed to show that both vectors will extend to the same position on either the x or y axis.

\$\endgroup\$
2
  • \$\begingroup\$ In what format do you have your input direction b? As an angle, as a unit vector, or as a vector of arbitrary length? \$\endgroup\$
    – DMGregory
    Apr 17 at 13:25
  • \$\begingroup\$ @DMGregory The only things known about b are its direction and origin point (it will start from the same place as a), so probably a vector with arbitrary length. \$\endgroup\$ Apr 18 at 17:55
1
\$\begingroup\$

Given an initial vector \$\vec b\$ of arbitrary length, we can calculate two scale factors that will bring it to the same horizontal or vertical offset as our reference vector \$\vec a\$:

$$s_x = \frac {a_x} {b_x}\\ s_y = \frac {a_y} {b_y}$$

Choose \$s_x\$ if you want both vectors to arrive at the same vertical line (same horizontal offset), or \$s_y\$ if you want both vectors to arrive at the same horizontal line. Or you could select whichever of the two is the finite positive number closest to zero, breaking ties arbitrarily.

Let's say I choose \$s_x\$, then my new modified vector is:

$$\vec b^\prime\ = s_x \vec b\ = \begin{bmatrix}s_x b_x\\ s_x b_y\end{bmatrix}= \begin{bmatrix}\frac{a_x}{b_x} b_x\\ \frac{a_x}{b_x} b_y\end{bmatrix}=\begin{bmatrix}a_x\\ \frac{a_x}{b_x} b_y\end{bmatrix}$$

The magnitude of this vector is just:

$$||\vec b ^\prime|| = s_x ||\vec b||$$

\$\endgroup\$
1
\$\begingroup\$

Fundamentally, you have a right-angle triangle

enter image description here

Your vector a is (conveniently) the adjacent side

You want to know the length of the hypotenuse which is coincident with b.

θ is the angle between your two vectors which we can get by calculating their dot product.

So I can't see any simpler option than

magnitude(a)/cos(dot(a, b))

Note that in your question, you alternate which vector is extended and which is attached to a right-angle. The above assumes you know a and want to find the magnitude of b.

\$\endgroup\$
1
  • 1
    \$\begingroup\$ It's strange to see a dot product used as an argument to the cosine function, since the dot product already returns a (scaled) cosine. I think you may want to review this formula to be sure it says what you meant it to. \$\endgroup\$
    – DMGregory
    Apr 17 at 13:27

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.