Refraction equation, quartic equation Given two points $P$ and $Q$, a line ($A$, $B$ - orthogonal projection of $P$, $Q$ onto the line) and a coefficient $n$, I want to find out such point $C$ that $\frac{\sin{a}}{\sin{b}}=n$ (in fact, it's an equation of light refraction). I also assume that $C$ lies between $A$ and $B$. See the image below:

What I know is: $w, h, d, n$. What I want to calculate is $x$.
I need to find the easiest solution possible, in order to calculate $x$ in a computer's application very efficiently.
What I've found out so far is:
$$n=\frac{\sin{a}}{\sin{b}}=\frac{\frac{d-x}{\sqrt{(d-x)^2+w^2}}}{\frac{x}{\sqrt{x^2+h^2}}}=\frac{(d-x)\sqrt{x^2+h^2}}{x\sqrt{(d-x)^2+w^2}}$$
Hence, we need to solve this equation for $x$ (assuming $0<x<d$):
$n^2x^2((d-x)^2+w^2)=(d-x)^2(x^2+h^2)$
which yields:
$f(x)=(n^2-1)x^4-2d(n^2-1)x^3+((d^2+w^2)(n^2-1)+w^2-h^2)x^2+2dh^2x-d^2h^2 = 0$
That way we obtained a quartic equation. How to solve it? I've tried using the Ferrari's solution, but the result is very complicated. Moreover, this equation can have as many as four real roots but I know it's got only one root for $0<x<d$. Do you have any idea how to simplify this equation or maybe you can suggest me an easier way to find $x$?
 A: A plot of a typical example shows that the right-hand side of your last displayed equation behaves rather nicely and it should be possible to obtain the solution efficiently using Newton's method, which wouldn't require drawing any roots and should yield the result to sufficient accuracy within a few iterations that only involve a couple of multiplications and additions and one division each.
You could use the solution $x=dh/(h+w)$ for $n=1$ as the initial value for the iteration.
By the way, I'd divide through by $d^4$ to get rid of the irrelevant scale and express everything relative to $d$:
$$\epsilon\xi^4-2\epsilon\xi^3+((1+\omega^2)\epsilon+\omega^2-\eta^2)\xi^2+2\eta^2\xi-\eta^2=0\;,$$
with $\epsilon=n^2-1$, $\xi=x/d$, $\omega=w/d$ and $\eta=h/d$ with initial value $\xi_0=\eta/(\eta+\omega)$.
A: As long as you're using a numerical method, you might as well stick with a non-polynomial form of your equation that is more well-behaved. In particular, you could just solve
$$\frac{(d-x)/\sqrt{(d-x)^2+w^2}}{x/\sqrt{x^2+h^2}}=n$$
for $x$. The left-hand side is a monotonically decreasing function of $x$ between $0$ and $d$, because the numerator is nonnegative and decreasing and the denominator is nonnegative and increasing. So you can just apply bisection search and it is guaranteed to work.
