I am working on a computer program and at some point I need to isolate an $x$ and a $z$. I am basically trying to isolate $x$ and $z$ in these two equations:
$xn_{x} + yn_{y} + zn_{z} = n_{d}$
$\sqrt{(x-e_{x})^{2} + (y-e_{y})^{2} + (z-e_{z})^{2}} = d$
I start by isolating $x$ in the first equation:
$xn_{x} + yn_{y} + zn_{z} = n_{d}$
$xn_{x} = n_{d} - yn_{y} - zn_{z}$
$x = \frac{n_{d} - yn_{y} - zn_{z}}{n_{x}}$
I then isolate $z$ in the second equation:
$(x-e_{x})(x-e_{x})+(y-e_{y})(y-e_{y})+(z-e_{z})(z-e_{z}) = d^{2}$
$x^{2}-2xe_{x}+(e_{x})^{2}+y^{2}-2ye_{y}+(e_{y})^{2}+z^{2}-2ze_{z}+(e_{z})^{2} = d^2$
$x^{2}-2xe_{x}+z^{2}-2ze_{z}+(e_{x})^{2}+(e_{z})^{2}+y^{2}-2ye_{y}+(e_{y})^{2} = d^2$
$x^{2}-2xe_{x}+z^{2}-2ze_{z}=d^2 - (e_{x})^{2}-(e_{z})^{2}-y^{2}+2ye_{y}-(e_{y})^{2}$
I replace $x$ by $(\frac{n_{d} - yn_{y} - zn_{z}}{n_{x}})$:
$$(\frac{n_{d} - yn_{y} - zn_{z}}{n_{x}})(\frac{n_{d} - yn_{y} - zn_{z}}{n_{x}})-2(\frac{n_{d} - yn_{y} - zn_{z}}{n_{x}})e_{x}+z^{2}-2ze_{z}=d^2 - (e_{x})^{2}-(e_{z})^{2}-y^{2}+2ye_{y}-(e_{y})^{2}$$
I expand:
$$\frac{(n_{d})^{2} - 2n_{d}yn_{y}-2n_{d}zn_{z}+(yn_{y})^{2}+2yn_{y}zn_{z}+(zn_{z})^{2}}{(n_{x})^{2}}-\frac{2e_{x}n_{d}-2e_{x}yn_{y}-2e_{x}zn_{z}}{n_{x}}+z^{2}-2ze_{z}=d^2 - (e_{x})^{2}-(e_{z})^{2}-y^{2}+2ye_{y}-(e_{y})^{2}$$
I get a formula in a form close to $az^{2}+bz+c=0$:
$$z^{2}+\frac{z^{2} + 2zn_{z}+(n_{z})^{2}}{(n_{x})^{2}} + \frac{-2n_{d}zn_{z}+2yn_{y}zn_{z}}{(n_{x})^{2}}-2ze_{z}+\frac{2e_{x}zn_{z}}{n_{x}}-d^2 + (e_{x})^{2}+(e_{z})^{2}+y^{2}-2ye_{y}+(e_{y})^{2}+\frac{(n_{d})^{2}-2n_{d}yn_{y}+y^{2}+2yn_{y}+(n_{y})^{2}}{(n_{x})^{2}}-\frac{2e_{x}n_{d}-2e_{x}yn_{y}}{n_{x}}=0$$
At this point, I do $z = \frac{-b\pm\sqrt{b^{2} - 4ac}}{2a}$ where:
$$a = 1+\frac{1}{(n_{x})^{2}}$$
$$b = \frac{2n_{z}}{(n_{x})^{2}} + \frac{-2n_{d}n_{z}+2yn_{y}n_{z}}{(n_{x})^{2}} - 2e_{z} + \frac{2e_{x}n_{z}}{n_{x}}$$
$$c = \frac{(n_{z})^{2}}{(n_{x})^{2}} - d^{2}+(e_{x})^{2}+(e_{y})^{2}+(e_{z})^{2}+y^{2}-2ye_{y}-\frac{2e_{x}n_{d}-2e_{x}yn_{y}}{n_{x}} + \frac{(n_{d})^{2}-2n_{d}yn_{y}+y^{2}+2yn_{y}+(n_{y})^{2}}{(n_{x})^{2}}$$
At this point I know I have a mistake because in my computer program, $z$ is not what it should be. I have tried a lot but still haven't managed to find out where my mistake is.
Hint.
Handle it as a geometric problem: given a plane $\Pi$ and a sphere $S$ determine the intersection circle $C$ with
$$ \cases{ p = (x,y,z)\\ \Pi\to (p-p_0)\cdot \vec n = 0\\ S\to ||p-e|| = d} $$
obviously, the problem has solution only if
$$ \left|(e-p_0)\cdot\frac{\vec n}{||\vec n||}\right|\le d $$
Here $p_0$ is any vector obeying $p_0\cdot\vec n = n_d$