# Given an ellipse and a reference point, how to find the two lines that are tangent to the ellipse?

I have an ellipse, possibly rotated and shifted from the origin, which is given by a parametrization similar to this one: \begin{aligned} x &= x_0 + a\cos\theta\cos\alpha - b\sin\theta\sin\alpha \\ y &= y_0 + a\cos\theta\sin\alpha + b\sin\theta\cos\alpha \\ \end{aligned}

where $$(x_0,y_0)$$ is the center of the ellipse, $$(a,b)$$ are its semi-axes, $$\alpha$$ is the rotation angle and $$\theta\in [0,2\pi]$$.

The reference point is given by $$(p_0,q_0)$$. Now I want to determine the parameters of the lines $$y = mx + h$$ that are tangent to the ellipse and go through that reference point. To do so, I compute $$h$$ from the reference point, $$h = q_0-mp_0$$, and then plug the ellipse equations into the line equation: $$\underbrace{y_0-q_0}_{\equiv q} + a\cos\theta\sin\alpha + b\sin\theta\cos\alpha = m\left(\underbrace{x_0-p_0}_{\equiv p} + a\cos\theta\cos\alpha - b\sin\theta\sin\alpha \right)$$ From this I can get an equation for the slope $$m$$ in dependence on $$\theta$$. I also know that the slope of a tangent to the ellipse is given by $$\frac{y'(\theta)}{x'(\theta)}$$, i.e.: $$m = \frac{a\sin\theta\sin\alpha - b\cos\theta\cos\alpha}{a\sin\theta\cos\alpha + b\cos\theta\sin\alpha}$$

Now, if I equate the two above equations for $$m$$, I get an expression which can be solved for $$\theta$$. I would expect that there are two possible solutions to this equations, as there are two tangent lines. I used WolframAlpha to solve this equation (see here), but it gave me four different solutions (which differ in the combination of two signs); I'm including a screenshot of one of the solutions below since it's quite long ($$x\equiv\theta, t\equiv\alpha$$): The four solutions emerge as $$x = \pm\cos^{-1}((\pm\sqrt{\dots}$$. There are no additional constraints mentioned, so something must have gone wrong, but I can't figure out what. I verified that two of those four solutions are valid with the help of some examples (testing various reference points p0,q0):

import matplotlib.pyplot as plt
from matplotlib.patches import Ellipse
import numpy as np
from numpy import arccos, cos, pi, sign, sin, sqrt

a, b = 3, 1  # ellipse parameters
center = 1, 2  # center of ellipse
angle = 60  # rotation of ellipse (counter-clockwise) [deg]
p0, q0 = -2, 3  # the reference point (x-,y-coordinates)

p = center - p0
q = center - q0
t = angle/180*pi
thetas = [
s1*arccos((s2*sqrt(a**2*b**4*(2*p*cos(t)**3 + 2*p*sin(t)**2*cos(t) + 2*q*sin(t)**3 + 2*q*sin(t)*cos(t)**2)**2 + 4*a**2*(-b**2*sin(t)**4 - b**2*cos(t)**4 - 2*b**2*sin(t)**2*cos(t)**2 + p**2*sin(t)**2 - 2*p*q*sin(t)*cos(t) + q**2*cos(t)**2)*(a**2*p**2*sin(t)**2 - 2*a**2*p*q*sin(t)*cos(t) + a**2*q**2*cos(t)**2 + b**2*p**2*cos(t)**2 + 2*b**2*p*q*sin(t)*cos(t) + b**2*q**2*sin(t)**2)) - a*b**2*(2*p*cos(t)**3 + 2*p*sin(t)**2*cos(t) + 2*q*sin(t)**3 + 2*q*sin(t)*cos(t)**2))/(2*(a**2*p**2*sin(t)**2 - 2*a**2*p*q*sin(t)*cos(t) + a**2*q**2*cos(t)**2 + b**2*p**2*cos(t)**2 + 2*b**2*p*q*sin(t)*cos(t) + b**2*q**2*sin(t)**2)))
for s1 in [-1, 1] for s2 in [-1, 1]
]

fig, ax = plt.subplots()
ax.add_patch(Ellipse(center, 2*a, 2*b, angle, color='tab:blue', alpha=0.4))
ax.scatter([p0], [q0], color='tab:blue', s=50)
for theta in thetas:
ex = center + a*cos(theta)*cos(t) - b*sin(theta)*sin(t)
ey = center + a*cos(theta)*sin(t) + b*sin(theta)*cos(t)
ax.axline((p0,q0), (ex,ey), color='tab:orange', lw=1)
ax.scatter([ex], [ey], color='tab:orange', s=25)
plt.show() So it seems that all that's left is to find a way how to pick the right combinations of signs for the two tangent points. However, I don't know what condition I should apply. I tried to discriminate based on the quadrant of $$(p_0-x_0,q_0-y_0)$$, but that didn't work. Also, since there are 6 different ways to pick 2 out of 4 possible sign combinations, it seems that the condition should include 6 different "sections/intervals".

• Please refer to my post using pole-and-polar relation and also Joachimsthal's notations in my post here and here. Jan 20, 2022 at 8:38
• You may want to refer to this page for some details. Jan 20, 2022 at 11:22
• I believe you can solve problem in other way. Let consider the slope of straight lines from ellipse point to reference point. This slope can be expressed in terms of $\theta$. Then you can find set of all possible slopes for all values of $\theta$. Boundary points of this set are slopes of tangent lines. If there is only one boundary point (for example, slopes set is $(-\infty;1]$ then one of tangent lines is vertical. The set can be discontinuous like $(-\infty;1]\cup[5;+\infty)$, then slopes of tangent lines are 1 and 5. Jan 20, 2022 at 15:17
• @IvanKaznacheyeu That seems like an interesting approach, but how would I find this set of slopes for a practical application? Jan 24, 2022 at 10:34

Let $$x=x_0+a\cos\theta\cos\alpha-b\sin\theta\sin\alpha$$, $$y=y_0+a\cos\theta\sin\alpha+b\sin\theta\cos\alpha$$ is ellipse point with parameter $$\theta\in[0;2\pi)$$. Then slope of line going through this point and reference point $$(p_0,q_0)$$ is $$k=\frac{y-q_0}{x-p_0}=\frac{y_0-q_0+a\cos\theta\sin\alpha+b\sin\theta\cos\alpha}{x_0-p_0+a\cos\theta\cos\alpha-b\sin\theta\sin\alpha}=\frac{A+B\cos\theta+C\sin\theta}{D+E\cos\theta+F\sin\theta}$$

$$\frac{dk}{d\theta}=\frac{CE-BF+(CD-AF)\cos\theta+(AE-BD)\sin\theta}{(D+E\cos\theta+F\sin\theta)^2}.$$

Tangent points will be determined by $$CE-BF+(CD-AF)\cos\theta+(AE-BD)\sin\theta=0$$ $$\frac{(CD-AF)\cos\theta+(AE-BD)\sin\theta}{\sqrt{(CD-AF)^2+(AE-BD)^2}}=\frac{BF-CE}{\sqrt{(CD-AF)^2+(AE-BD)^2}}$$ $$\cos\beta=\frac{AE-BD}{\sqrt{(CD-AF)^2+(AE-BD)^2}},\; \sin\beta=\frac{CD-AF}{\sqrt{(CD-AF)^2+(AE-BD)^2}}$$ $$\sin(\theta+\beta)=\frac{BF-CE}{\sqrt{(CD-AF)^2+(AE-BD)^2}}$$ $$\theta_1=-\beta+\arcsin\frac{BF-CE}{\sqrt{(CD-AF)^2+(AE-BD)^2}},$$ $$\theta_2=\pi-\beta-\arcsin\frac{BF-CE}{\sqrt{(CD-AF)^2+(AE-BD)^2}}$$

The calculation procedure: given $$x_0$$, $$y_0$$, $$a$$, $$b$$, $$\alpha$$, $$p_0$$, $$q_0$$. Calculate $$A$$, $$B$$, $$C$$, $$D$$, $$E$$, $$F$$. Then calculate $$\cos\beta$$, $$\sin\beta$$, $$\sin(\theta+\beta)$$ by formulae from above. Then find $$\beta$$ and two values of $$\theta$$. Then calculate $$(x,y)$$ for both tangent points, using $$\theta_1$$ and $$\theta_2$$.

• We have $\cos\beta = \dots$ and $\sin\beta = \dots$, so it seems there are two ways to determine the value for $\beta$ and they should be equivalent? Because when I plug in the specific values from my example, they differ by $\approx 3$. Jan 24, 2022 at 20:35
• $\cos\beta=c$ determines two values of $\beta \in [0;2\pi)$ (except $c=\pm 1$. $\sin\beta =s$ determines two values of $\beta \in [0;2\pi)$ (except $s=\pm 1$. But simultaneous $\cos\beta=c$ and $\sin\beta=s$, where $c^2+s^2=1$ determines only one value of $\beta \in [0;2\pi)$. You cannot just use $\arccos c$ or $\arcsin s$ to find $\beta$, you need to account both conditions. Jan 25, 2022 at 12:22
• You're right, now it works for me. Thanks. Jan 25, 2022 at 13:25

One way is to use the equation which is $$(\frac{(x-x_0)\cos(\alpha)+(y-y_0)\sin(\alpha)}{a})^2+(\frac{-(x-x_0)\sin(\alpha)+(y-y_0)\cos(\alpha)}{b})^2-1=0.$$

Then the equation of the line pair (tangents from $$(p_0,q_0)$$) are by joachimsthal $$s\cdot s_{11}-s_1^2=0$$ or

$$((\frac{(x-x_0)\cos(\alpha)+(y-y_0)\sin(\alpha)}{a})^2+(\frac{-(x-x_0)\sin(\alpha)+(y-y_0)\cos(\alpha)}{b})^2-1)((\frac{(p_0-x_0)\cos(\alpha)+(q_0-y_0)\sin(\alpha)}{a})^2+(\frac{-(p_0-x_0)\sin(\alpha)+(q_0-y_0)\cos(\alpha)}{b})^2-1)\\-((\sin(\alpha)^2/b^2+\cos(\alpha)^2/a^2)xp_0+((\cos(\alpha)\sin(\alpha))/a^2-(\cos(\alpha)\sin(\alpha))/b^2)(xq_0+p_0y)+(\cos(\alpha)^2/b^2+\sin(\alpha)^2/a^2)yq_0+((\cos(\alpha)\sin(\alpha)y_0)/b^2-(\cos(\alpha)\sin(\alpha)y_0)/a^2-(\sin(\alpha)^2x_0)/b^2-(\cos(\alpha)^2x_0)/a^2)(x+p_0)+((-(\cos(\alpha)^2y_0)/b^2)-(\sin(\alpha)^2y_0)/a^2+(\cos(\alpha)\sin(\alpha)x_0)/b^2-(\cos(\alpha)\sin(\alpha)x_0)/a^2)(y+q_0)+((\cos(\alpha)^2y_0^2)/b^2+(\sin(\alpha)^2y_0^2)/a^2-(2\cos(\alpha)\sin(\alpha)x_0y_0)/b^2+(2\cos(\alpha)\sin(\alpha)x_0y_0)/a^2+(\sin(\alpha)^2x_0^2)/b^2+(\cos(\alpha)^2x_0^2)/a^2-1))^2=0$$

which simplifies to

$$\frac{((y_0-q_0)^2-((b^2-a^2)\cos(2α)+a^2+b^2)/2)(x-p_0)^2+(\sin(2α)(a^2-b^2)-2(x_0-p_0)(y_0-q_0))(x-p_0)(y-q_0)+((x_0-p_0)^2+((b^2-a^2)\cos(2α)-(a^2+b^2))/2)(y-q_0)^2}{a^2b^2}=0$$ which you can factor by setting $$t=\frac{x-p_0}{y-q_0}$$ (or its inverse) and using the quadratic formula to get

$$(x-p_0)-(y-q_0)\frac{2(x_0-p_0)(y_0-q_0)+\sin(2α)(b^2-a^2)\pm\sqrt{D}}{2(y_0-q_0)^2-((b^2-a^2)\cos(2α)+b^2+a^2)}=0$$

where

$$D=2(a^2+b^2+(a^2-b^2)\cos(2\alpha))(y_0-q_0)^2 +4(b^2-a^2)\sin(2\alpha)(x_0-p_0)(y_0-q_0) +2((b^2-a^2)\cos(2\alpha)+b^2+a^2)(x_0-p_0)^2-4a^2b^2$$ • The equation you provide looks like another ellipse equation ($Ax^2 + Bxy + Cy^2 = 0$), so I cannot imagine how this defines two lines. Could you elaborate on this? When using $t = \frac{x-p_0}{y-q_0}$ then this prohibits $y=q_0$ which is however required by the fact that line should pass through the reference point $(p_0,q_0)$. This seems like a contradiction. Jan 24, 2022 at 10:29
• @a_guest An example: $(x-3)(2y-x-5)=0$ is the line pair $-x^2+2xy-2x-6y+15=0$ which meet in $(3,4)$ i.e. $2(x-3)(y-4)-(x-3)^2=0.$ Setting $t=(x-3)/(y-4)$ we get $(2t-t^2)(y-4)^2=0.$ Solving you get $-(y-4)^2 t(t-2)=0$ or $-((x-3)/(y-4))((x-3)/(y-4)-2)(y-4)^2$ or $-(x-3)((x-3)-2(y-4)))$ i.e. you recover the lines. The general case is similar. Jan 24, 2022 at 10:48
• Well, for this example you already start from a factorized version which is the product of two linear equations. But in general not every quadratic form can be factorized into two linear equations, so it's still not clear to me how the equation in your answer represents two lines. Also, the $t$-trick seems to have the problem that it is not defined for $y=q_0$. Jan 24, 2022 at 12:05
• @a_guest Let's take a general example then, $x^2/9+y^2/4-1=0$ and the point $(-2,5)$ the equation of the tangent line pair is $(x^2/9+y^2/4-1)((-2)^2/9+5^2/4-1)-(x\cdot (-2)/9+y\cdot 5/4-1)^2=0$ or $\frac{21 \; x^{2} + 20 \; x \; y - 16 \; x - 5 \; y^{2} + 90 \; y - 241}{36}=0$ or $\frac{-(5(y-5)^2-20(x+2)(y-5)-21(x+2)^2)}{36}=0$ or $5t^2-20t-21=0$ which gives $-\frac5{36}(t+(\sqrt{205}-10)/5)(t-(\sqrt{205}+10)/5)$ or $((y-5)+(x+2)(\sqrt{205}-10)/5)((y-5)-(x+2)(\sqrt{205}+10)/5)=0.$ You can check with e.g. geogebra that these are the two tangent lines. Jan 24, 2022 at 12:21
• Well, it seems that sometimes I need to triple check my code; eventually I found a sign flip for one of the terms. Now your solutions works for me as well :-) So after all, this is a practicable solution which meets my needs. Nevertheless, I'll leave the question open for now, as I'm also interested in how to fix the solution that I obtained with my approach (in terms of choosing the right signs); perhaps someone still knows an answer to that. By the way, the resulting equation can still be simplified in terms of trigonometric equality and collecting terms into $(x_0-p_0)^2, (y_0-q_0)^2$ Jan 24, 2022 at 13:16

There can be only two solutions.

You can make the problem much simpler by transforming the ellipse to a circle (rotate by $$-\alpha$$ and stretch by $$(\frac1a,\frac1b)$$), and move it to the origin. Apply the same transformation to the external point.

Now you can solve by simple trigonometry or express that the point of contact defines a right angle:

$$(p-\cos t)\cos t+(q-\sin t)\sin t=0$$

or $$\cos\left(t-\arctan\frac qp\right)=\frac1{\sqrt{p^2+q^2}}.$$

• This is an elegant solution, but I believe the resulting values for theta are given by $\theta = 2\arctan\left(\frac{q_0 \pm \sqrt{p_0^2+q_0^2-1}}{p_0+1}\right)$. Jan 24, 2022 at 20:21
• @a_guest: you are right, but I cannot spot what I did wrong.
– user1015917
Jan 25, 2022 at 8:13
• Well, the perpendicularity constraint is given by $(\vec{e}-\vec{p})\cdot\vec{e} = 0$ where $\vec{p}$ is the reference point and $\vec{e}$ is the point on the ellipse. This gives $\vec{e}^2 - \vec{p}\cdot\vec{e} = 0$ which simplifies to $1 - p_0\cos\theta - q_0\sin\theta = 0$. Solving for $\theta$ yields the above result. Jan 25, 2022 at 10:12