2

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$):

Example solution

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[0] - p0 q = center[1] - q0 t = angle/180pi thetas = [ s1arccos((s2sqrt(a2b4(2p*cos(t)3 + 2psin(t)2cos(t) + 2q*sin(t)3 + 2qsin(t)cos(t)2)2 + 4a2*(-b2sin(t)4 - b2cos(t)4 - 2*b2sin(t)2cos(t)2 + p2sin(t)2 - 2pqsin(t)cos(t) + q2cos(t)2)*(a2p2sin(t)2 - 2*a2pqsin(t)cos(t) + a2*q2cos(t)2 + b2p2*cos(t)2 + 2b2pqsin(t)cos(t) + b2q2*sin(t)2)) - ab2(2pcos(t)3 + 2psin(t)2cos(t) + 2qsin(t)3 + 2qsin(t)cos(t)2))/(2*(a2p2sin(t)2 - 2*a2pqsin(t)cos(t) + a2*q2cos(t)2 + b2p2*cos(t)2 + 2b2pqsin(t)cos(t) + b2q2*sin(t)2))) for s1 in [-1, 1] for s2 in [-1, 1] ]

fig, ax = plt.subplots() ax.add_patch(Ellipse(center, 2a, 2b, angle, color='tab:blue', alpha=0.4)) ax.scatter([p0], [q0], color='tab:blue', s=50) for theta in thetas: ex = center[0] + acos(theta)cos(t) - bsin(theta)sin(t) ey = center[1] + acos(theta)sin(t) + bsin(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()

Example plot

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".

a_guest
  • 211
  • Please refer to my post using pole-and-polar relation and also Joachimsthal's notations in my post here and here. – Ng Chung Tak Jan 20 '22 at 08:38
  • You may want to refer to this page for some details. –  Jan 20 '22 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. – Ivan Kaznacheyeu Jan 20 '22 at 15:17
  • @IvanKaznacheyeu That seems like an interesting approach, but how would I find this set of slopes for a practical application? – a_guest Jan 24 '22 at 10:34

4 Answers4

2

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)$. – a_guest Jan 24 '22 at 20:21
  • @a_guest: you are right, but I cannot spot what I did wrong. –  Jan 25 '22 at 08: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. – a_guest Jan 25 '22 at 10:12
1

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$$

tangents

  • 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. – a_guest Jan 24 '22 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-Magnus Økland Jan 24 '22 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$. – a_guest Jan 24 '22 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-Magnus Økland Jan 24 '22 at 12:21
  • This is not a general example, it is a specific example, as all the coefficients in your example are fixed. A general example would be of the form $Ax^2+Bxy+Cy^2=0$. If we ignore the $y=0$ problem when defining $t=\frac{x}{y}$ for the moment, then this is equivalent to $At^2+Bt+C=0$ which has a solution in the real numbers iff $B^2-4AC\geq 0$. In fact, in order to obtain two different lines, we need $B^2-4AC>0$. How is this guaranteed for the equation in your answer? When I try the example parameters from my question and plug them into this equation, I get $B^2-4AC<0$ which has no solution. – a_guest Jan 24 '22 at 12:44
  • @a_guest It's more general than my first example and less general than the answer. If you choose a point inside the ellipse you won't get real tangent lines. And this is exactly when the approach fails. Do you want I share the geogebra file screenshotted in the answer? – Jan-Magnus Økland Jan 24 '22 at 12:46
  • This is what I expected as well (though I'd still like to see a proof for that). But clearly, as you can see from the plot in my question, the reference point does not lie inside the ellipse. I double checked my implementation and couldn't spot any error. By the way, how did you obtain the simplified version of the equation? It seems that more simplifications are possible, e.g. $-a^2 + cos(\alpha)^2a^2 = -a^2\sin(\alpha)^2$, so I'm wondering how this form was obtained. – a_guest Jan 24 '22 at 12:50
  • @a_guest Programmatically of course (maple). I can share the .ggb when I get home from work. – Jan-Magnus Økland Jan 24 '22 at 12:52
  • 1
    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$ – a_guest Jan 24 '22 at 13:16
  • @a_guest Yes, see the edit. – Jan-Magnus Økland Jan 24 '22 at 18:08
1

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$.

Ivan Kaznacheyeu
  • 4,298
  • 5
  • 10
  • 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$. – a_guest Jan 24 '22 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. – Ivan Kaznacheyeu Jan 25 '22 at 12:22
  • You're right, now it works for me. Thanks. – a_guest Jan 25 '22 at 13:25
0

Your given ellipse is of the form

$ p(t) = C + V u $

where

$ V = [v_1, v_2] , u = [\cos t , \sin t ]^T $

Here, $v_1 = \begin{bmatrix} a \cos \theta \\ a \sin \theta \end{bmatrix} \hspace{10pt} v_2 = \begin{bmatrix} - b \sin \theta \\ b \cos \theta \end{bmatrix} $

$\theta$ is the rotation angle of the ellipse, and $t$ is the parameter.

Note that

$ V = R D $

where

$ R = \begin{bmatrix} \cos \theta && - \sin \theta \\ \sin \theta && \cos \theta \end{bmatrix} \hspace{10pt} D = \begin{bmatrix} a && 0 \\ 0 && b \end{bmatrix} $

Consider the transformation matrix

$ A = D^{-1} R^T $

Apply this to the point $p$, you get

$ p' = A p = D^{-1} R^T C + u $

So that $p'$ lies on a circle with center at $ C' = D^{-1} R^T C $. Apply the same transformation to the reference point $P = (p_0, q_0) $, you get its image given by

$ Q = A P = D^{-1} R^T P $

Note that the linear transformation $A$ preserves the tangency between the original ellipse, and its transformed image which is the unit circle centered at $C'$.

Let $d$ be the distance between $Q$ and $C'$, then

$ d = \| D^{-1} R^T P - D^{-1} R^T C \| = \| D^{-1} R^T (P - C) \| $

We want to draw two tangents from $Q$ to the unit circle centered at $C'$. So define the angle

$ \phi = \sin^{-1} \left( \dfrac{1}{d} \right) $

Then $\phi$ is the angular displacement of the two tangents from the line connecting $Q$ and $C'$. Define the vector $V = C' - Q = (V_x, V_y)$, and let $\alpha$ be the angle this vector makes with the positive $x$ axis, then

$ \alpha = atan2(V_x, V_y) $

where $atan2(x_1, x_2)$ returns the angle $\psi$ such that

$ \cos \psi =\dfrac{x_1}{\sqrt{x_1^2+x_2^2}}$ and $\sin \psi = \dfrac{x_2}{\sqrt{x_1^2+x_2^2} }$

And let $ m = tan(\alpha) $

The slopes of the two tangents are easy to calculate, let their angles be $\beta_1 $ and $\beta_2$ , then

$ \beta_1 = \alpha + \phi $ , $ \beta_2 = \alpha - \phi $

Therefore, the slopes are

$ m_1 = \tan(\beta_1) = \dfrac{ m + f }{1 - m f} \hspace{10pt} $

$ m_2 = \tan(\beta_2) = \dfrac{ m - f}{1 + m f} $

where $ f = \tan(\phi) $

We have a common point on the two tangents which is $Q$ and their slopes $m_1$ and $m_2$. If $Q = (q_1, q_2)$ then the equation of the first tangent is

$ y = q_2 + m_1 (x - q_1) $

And the equation of the second tangent is

$ y = q_2 + m_2 (x - q_1) $

These two equations can be written in vector form as

$ n_1^T (r' - Q) = 0 $

and

$ n_2^T (r' - Q) = 0 $

where $ r' = (x, y) $ and $ n_1 = [m_1, -1], n_2 = [m_2, -1] $ are the normal vectors to the two tangents.

Finally, recall that $ r' = A r = D^{-1} R^T r $. Therefore, the equations of the two tangents to the ellipse are

$ n_1^T ( D^{-1} R^T r - Q ) = 0 $

and

$n_2^T (D^{-1} R^T r - Q ) = 0 $

But $Q = D^{-1} R^T P $

Hence, the equations of the two tangents can be written as

$ ( R D^{-1} n_1 )^T (r - P) = 0 $

And

$ ( R D^{-1} n_2 )^T (r - P) = 0 $


Appendix:

The two tangency point for the unit circle are given by

$ T'_1 = C' + [\cos(\alpha + \pi - ( \dfrac{\pi}{2} - \phi) ) , \sin (\alpha + \pi - ( \dfrac{\pi}{2} - \phi ) ) ] $

$T'_2 = C' + [ \cos(\alpha + \pi + ( \dfrac{\pi}{2} - \phi) ), \sin (\alpha + \pi + (\dfrac{\pi}{2} - \phi) ) ] $

These two expressions simplify to

$ T'_1 = C' + [ - \sin( \alpha + \phi ) , \cos (\alpha + \phi ) ] $

$ T'_2 = C' + [ \sin ( \alpha - \phi) , - \cos (\alpha - \phi) ] $

The corresponding tangency point in the original space (before transformation) are

$ T_1 = A^{-1} T'_1 = R D T'_1 $

$ T_2 = A^{-1} T'_2 = R D T'_2 $

The figure below shows an example that uses the formulas developed above, for an ellipse with semi-major axis of $a = 5$ and semi-minor axis of $b = 2$, and an angle of inclination of $\theta = 45^\circ$. The center is at $C = (3, -1)$ and the reference points is $P = (12, -3) $. The two tangent lines and the tangency points are shown.

enter image description here