Adding 2 vectors and keeping the resulting magnitude below a max value I have 2 vectors, $\vec{A}=(x, y)$ and $\vec{B}=(u, v)$. I also have a max value $M$. I want to add the vectors together so that the resulting magnitude is no greater than $M$, by multiplying $\vec{B}$ by a certain factor $f$.
Specifically, I am looking for the largest possible $f$ such that $\| \vec{A} + f\vec{B} \| \leq M$.
Please note that in my situation, $\| \vec{A} \| \leq M$, which means $0 \leq f$. I also want $f\leq1$, because in my situation it doesn't make sense to multiply $\vec{B}$ by a factor greater than 1.
To find $f$, I proceed as follows. In the case where $\| \vec{A} + \vec{B} \| \leq M$, I can take $f=1$ and be done. If that's not the case, then I can use the definition of vector magnitude and solve the following equation for $f$:
$$
M=\sqrt{(x+fu)^2+(y+fv)^2}
$$
Using WolframAlpha, I get the following 2 solutions:
$$
f=\frac{\pm\sqrt{(2ux+2vy)^2-4(u^2+v^2)(-M^2+x^2+y^2)} -2ux-2vy}{2(u^2+v^2)}
$$
This is a somewhat involved equation. My question is: is there an easier / simpler way to find $f$? This is for physics implementation in a game and any optimization helps with performance, as in my case this needs to be calculated several times per frame.
Thanks!
 A: I'm going to use $t$ instead of $f$.
The points $A + tB$ lie on a line through the point $A$ in the direction of the vector $B$. You are trying to find the values of $t$ that give you the intersections of this line with a circle $C$ of radius $M$ centered at the origin. 
If the point $A$ lies inside the circle $C$, there will be two values of $t$, one negative and one positive. If the point $A+B$ lies outside the circle $C$, then the positive $t$ value will be less than $1$.
So how to find the desired value of $t$.
We have:
\begin{align}
&A + tB \;\text{  lies on the circle  } C  \\
\Leftrightarrow\;& \|A + tB\| = M          \\
\Leftrightarrow\;& \|A + tB\|^2 = M^2      \\
\Leftrightarrow\;& (A + tB) \cdot (A + tB) = M^2  \\
\Leftrightarrow\;& A \cdot A + 2t(A \cdot B) + t^2(B.\cdot B) = M^2  
\end{align}
The desired solution of the quadratic is:
$$
t = \frac{-A \cdot B + \sqrt{ (A \cdot B)^2 - (B.B) (A \cdot A - M^2)}  } {  B \cdot B}
$$
The necessary code would be:
double AA = x*x + y*y;     // A dot A
double BB = u*u + v*v;     // B dot B
double AB = u*x + v*y;     // A dot B

double d = sqrt(AB*AB - BB*(AA - M*M));

double t = (d - AB)/BB;

This is roughly the same formula you derived, but in a rather tidier form (in my opinion), and with slightly less arithmetic. I don't think you can do any better than this. The only part that will take any time is the square root, and I think this is unavoidable. You should be able to do this calculation millions of times a second, even on a wristwatch. Iterative algorithms will be much slower, as you already discovered.
A: Iterative approximation to the required value of $f$ seems to fit to your needs, if I understood well.
You want$$
\left( {x + f\,u} \right)^{\,2}  + \left( {y + f\,v} \right)^{\,2}  - M^{\,2}  \le 0
$$
i.e.
$$
\,f^{\,2}  + 2\,{{\left( {x\,u\, + \,y\,v} \right)} \over {\left( {u^{\,2}  + \,v^{\,2} } \right)}}\,f + {{x^{\,2}  + y^{\,2}  - M^{\,2} } \over {\left( {u^{\,2}  + \,v^{\,2} } \right)}} \le 0
$$
where the left term is a vertical parabola, with axis at $
f =  - \,{{\left( {x\,u\, + \,y\,v} \right)} \over {\left( {u^{\,2}  + \,v^{\,2} } \right)}}
$ , etc.
Now fix $f=1$ and calculate the ordinate: if negative that is ok, if positive apply the Newton-Raphson iteration and in a few steps you will get a good approximation of the required value for $f$.
Adjust the algorithm to fix for precision / max no. of steps, banal values, etc.
A: Update: I tried playing with iterative approximation approaches (like G Cab mentioned) and doing a binary search to find a suitable value of $f$, but nothing could come close to the performance of simply calculating the equation directly.
I made sure to use any possible optimization (storing all intermediate steps of the calculation to reuse as much as possible, using multiplication rather than power to calculate the squares, etc).
So, unless there is an approach that doesn't involve the definition of vector magnitude, I don't think there's a better way to calculate $f$.
