I'd like to determine the ellipse that is tangent to the $x$ axis at $\mathbf{r_1} = (a, 0), a \gt 0$ and the $y$ axis at $\mathbf{r_2} = (0, b), b \gt 0$, and also tangent to the line $\mathbf{n}\cdot (\mathbf{r} - \mathbf{r_0}) = 0 $ at an unknown point $\mathbf{r_3}$, where $\mathbf{r} = [x, y]^T$ and $\mathbf{n}$ and $\mathbf{r_0}$ are given 2-vectors. This is the task.
My attempt:
The equation of the ellipse is
$ (\mathbf{r - C})^T Q (\mathbf{r - C}) = 1 $
where $\mathbf{C}=[C_x, C_y]^T$ is the unknown center, and $Q$ is $2 \times 2$ symmetric and positive definite, also unknown. That makes a total of $5$ unknowns.
The gradient of the above function of $\mathbf{r}$ is
$ g = 2 Q (\mathbf{r - C}) $
At $\mathbf{r_1}$ the gradient is pointing along the negative $\mathbb{j}$ direction, so that
$ Q ( \mathbf{r_1 - C}) = - k \ \mathbf{j} , k \gt 0$
It follows that
$ \mathbf{r_1 - C} = - k Q^{-1} \mathbf{j} \tag{1}$
Substituting this into the ellipse equation yields
$ k = \dfrac{1}{\sqrt{ \mathbf{j}^T Q^{-1} \mathbf{j} }} $
Now premultiplying $(1)$ by $ \mathbf{j}^T $ gives
$ \mathbf{j}^T ( \mathbf{r_1 - C}) = - C_y = - \sqrt{ \mathbf{j}^T Q^{-1} \mathbf{j} } $
So that
$ C_y = \sqrt{ \mathbf{j}^T Q^{-1} \mathbf{j} } \tag{2} $
Similarly,
$ C_x = \sqrt{ \mathbf{i}^T Q^{-1} \mathbf{i} } \tag{3} $
Now pre-multiplying $(1)$ by $\mathbf{i}^T$ yields
$ C_x - a = \dfrac{\mathbf{i}^T Q^{-1} \mathbf{j}}{\sqrt{ \mathbf{j}^T Q^{-1} \mathbf{j}}} \tag{4}$
So that
$ C_x C_y - a C_y = \mathbf{i}^T Q^{-1} \mathbf{j} \tag{5}$
and similarly,
$ C_y - b = \dfrac{ \mathbf{j}^T Q^{-1} \mathbf{i} } { \sqrt{ \mathbf{i}^T Q^{-1} \mathbf{i}}} \tag{6} $
so that
$ C_x C_y - b C_x = \mathbf{j}^T Q^{-1} \mathbf{i} \tag{7}$
Hence,
$ \dfrac{C_x} { C_y } = \dfrac{a}{b} \tag{8}$
Now if let
$ P = Q^{-1} = \begin{bmatrix} P_{11} && P_{12} \\ P_{12} && P_{22} \end{bmatrix} \tag{9}$
Then using equations $(2),(3),(8)$, we have,
$ b^2 P_{11} = a^2 P_{22} = t^2 \tag{10}$
Now,
$ P_{12} = \sqrt{P_{11} P_{22}} - b \sqrt{P_{11}} = \dfrac{t^2}{ab} - t \tag{11}$
Now considering the given tangent line:
At $\mathbf{r_3}$ which is unknown,
$ (r_3 - C) = - k P \mathbf{n} $ (remember, $P = Q^{-1} $ )
where it is assumed that $\mathbf{n}$ has positive $x$ and $y$ components.
Again, we get $ k = \dfrac{1}{\sqrt{ \mathbf{n}^T P \mathbf{n} } } $
Now, the equation of the line is $\mathbf{n}^T (r - \mathbf{r_0}) = 0 $, so
$ \mathbf{n}^T (\mathbf{r_3 - C}) = \mathbf{n}^T ( \mathbf{r_3} - \mathbf{r_0} + \mathbf{r_0} - \mathbf{C} ) = - \sqrt{ \mathbf{n}^T P \mathbf{n}} \tag{12}$
If we take $\mathbf{n} = [n_1, n_2]^T $ then this equation becomes,
$ n_1 ( r_{0x} - C_x ) + n_2 ( r_{0y} - C_y) = - \sqrt{ P_{11} n_1^2 + P_{22} n_2^2 + 2 P_{12} n_1 n_2 } \tag{13} $
If we define the constant $\alpha = \mathbf{n}^T \mathbf{r_0} $, then $(12)$ becomes
$ \left( (\dfrac{n_1}{b} + \dfrac{n_2}{a} ) t - \alpha \right)^2 = \dfrac{ n_1^2 t^2}{b^2} + \dfrac{n_2^2 t^2}{a^2} + 2 n_1 n_2 \left( \dfrac{t^2}{ab} - t \right) \tag{14} $
which simplifies to
$ - 2 \alpha (\dfrac{n_1}{b} + \dfrac{n_2}{a} ) t + \alpha^2 = - 2 n_1 n_2 t \tag{15}$
Multiplying through by $a b $ gives
$ - 2 \alpha ( a n_1 + b n_2 ) + \alpha^2 a b = - 2 n_1 n_2 a b t \tag{16}$
From which,
$ t = \dfrac{a b \alpha^2 } {2 \left( \alpha (an_1 + bn_2) - n_1 n_2 a b \right) } \tag{17}$
Having obtained $t$, all the parameters of the ellipse can now be computed directly.
I've written a small piece of code to test the above formula for $t$, and found that it is correct.
Here's an example with $a= 5, b = 3$, $\mathbf{n }= [1, 2]^T , \mathbf{r_0 } = [5, 6]^T $
Thank you all.