# Equation for an ellipse from 2 points and their tangents?

I'd like to get the equation of an ellipse given 2 points on the ellipse, and the slopes of the tangent lines at those 2 points. I've searched extensively, with most results being from this forum. I went over all the posts that sounded appropriate, including the one with an almost identical title, but none seemed directly implementable in a simple script (and some admit that they're getting very messy, symbolically). From the posts, it sounds like I'll need 5 independent pieces of information, and I'm only providing 4, and I couldn't see what 5th condition I could make up or discard. I went through symbolically using a derived slope equation and the basic ellipse equation, and that ended up giving me 4 equations in 4 unknowns (a,b,h,k), but I couldn't see a solution in this approach (maybe because equations started having cross products, and thus were now not a system of linear equations). I had tried implementing part of some post that solved for the equation assuming the center was at (0,0), so I eyeballed where my center might be (typically not 0,0). This ended up in trying to take the square root of a negative number, so I guess I made an inconsistent center pick.

So is a "straightforward" solution actually in the postings? I've been avoiding the polar coordinate route, so if that's the easiest way, please confirm. Thanks for any insight or guidance.

Update: To show what I've tried so far, I assume a non rotated (no x, y, or xy terms in the basic general conic equation) ellipse, and made an equation for each point. So, for point 1:

$$\frac{(x_1-h)^2}{a^2}+\frac{(y_1-k)^2}{b^2}=1\,\,\,\,\,(eq1)$$ And similarly for point 2 (eq2).

I then differentiated (eq1) to describe slope (m) (eq3 for point 1, eq4 for point 2):

$$m=\frac{dy}{dx}=-\frac{b^2}{a^2}\frac{(x_1-h)}{(y_1-k)}\,\,\,\,\,(eq3,4)$$

Solving eq1 and eq3 for $a^2$, I set them equal and got an equation in 3 unknowns (b,h,k) for each point (eq5 for point 1, eq6 for point 2):

$$b^2=(y_1-k)^2-m_1(x_1-h)(y_1-k)\,\,\,\,\,(eq5,6)$$ I then combined eq5 and eq6 (setting their right hand sides equal to each other) to get an equation in 2 unknowns (h,k). But now I don't see where to get another equation in h,k so I can solve for them. So any other approaches? Thanks.

• Use mathjax to improve your question. – Taha Akbari Aug 7 '16 at 17:41
• How? Is that a formatting code available in a post edit? Or do I make something offline and insert a picture? Thanks. – gkd720 Aug 7 '16 at 17:52
• – Taha Akbari Aug 7 '16 at 17:54
• I don’t really understand what you’re asking for here. You already know that you don’t have enough information to guarantee a unique solution, even if the ellipse is in standard position (what if the two tangents are parallel to a coordinate axis?). The extra information can be many things: another point on the ellipse, its center, axis angle, eccentricity, &c. – amd Aug 7 '16 at 21:49
• In some situations. the center is determined by the two points and their tangents. For example, if an ellipse passes through the two points $(1,0)$ and $(-1,0)$ and has vertical tangents there, then its center is at $(0,0)$. So saying that the center is at $(0,0)$ doesn't give you a fifth piece of information, and saying that the center is anywhere else is inconsistent. – Andreas Blass Aug 9 '16 at 1:06

I’ll sketch out an approach to solving this that can involve some messy algebra, but is conceptually straightforward. It uses some ideas and results from this paper by Alan Horwitz.

Begin by solving the simpler problem of finding an ellipse that’s tangent to the coordinate axes at $(1,0)$ and $(0,1)$. The solution is, of course, not unique: there’s a family of ellipses with these tangents. Besides the two tangents, these ellipses share some other features. Their centers all lie on the line $x=y$ and each ellipse is circumscribed by the square $[0,s]\times[0,s]$ for some unique $s>1$. We can thus take this $s$ as a parameter, which yields the following equation for the corresponding ellipse: $$sx'^2+sy'^2-2(s-2)x'y'-2s(x'+y')+s=0.\tag{1}$$ By symmetry, it’s easy to see that the center of the ellipse is at $\left(\frac s2,\frac s2\right)$ and with a bit of work one can calculate that the eccentricity is $\sqrt{s-2\over s-1}$ for $s\ge2$ and $\sqrt{2-s}$ for $1\lt s\le2$. The case $s=2$, of course, produces a circle. Note also that for $s\gt2$, the major axis lies along $x=y$, while for $s\lt2$ the minor axis is along this line.

To solve the general case, one need only apply an affine transformation to one of these ellipses. We’re given $P_1$ on the tangent line $\ell_1$ and $P_2$ on $\ell_2$. Assume in addition that $\ell_1\nparallel\ell_2$. Let $P_0$ be the intersection of the two lines, and let $T$ be the affine transformation that makes the following mappings: \begin{align}(0,0)&\mapsto P_0\\(1,0)&\mapsto P_1\\(0,1)&\mapsto P_2.\end{align} This transformation maps ellipses from the constructed family to ones that are tangent to $\ell_1$ at $P_1$ and to $\ell_2$ at $P_2$. The bounding squares are mapped to parallelograms defined by $P_0$, $P_0+s(P_1-P_0)$ and $P_0+s(P_2-P_0)$. That is, the parallelogram has a vertex at $P_0$ and sides that lie along $\ell_1$ and $\ell_2$ with lengths equal to the corresponding line segments scaled by a factor of $s$. This parallelogram circumscribes the image of the ellipse for the same value of $s$. Thus, even in the general case, $s$ makes sense as a linear “size” parameter for the resulting ellipse. Note that, although $T$ preserves the center, bounding parallelogram and tangent points, it does not preserve the major and minor axes of the ellipse. (The center of the ellipse coincides with the center of the parallelogram, so “eyeballing” it will likely result in inconsistent constraints: these centers all lie along the image of $x=y$, the line through $P_0$ and the opposite corner of the parallelogram.)

Determining the affine map $T$ is almost trivial. In homogeneous coordinates, it can be represented by the matrix $$M=\begin{bmatrix}\mid&\mid&\mid\\P_1-P_0&P_2-P_0&P_0\\\mid&\mid&\mid\end{bmatrix}=\begin{bmatrix}x_1-x_0&x_2-x_0&x_0\\y_1-y_0&y_2-y_0&y_0\\0&0&1\end{bmatrix}$$ which encodes the mapping \begin{align}x&=(x_1-x_0)x'+(x_2-x_0)y'+x_0\\y&=(y_1-y_0)x'+(y_2-y_0)y'+y_0.\end{align} Make the appropriate substitutions in equation (1) to get an equation for the mapped ellipse. Depending on how you’re doing this, it might be easier to work with another form of the equation, such as the vector form $(\mathbf x-\mathbf p)^TA(\mathbf x-\mathbf p)=1$, where $\mathbf p$ is the center of the ellipse and $A$ is a positive definite matrix.

If you don’t want to fiddle with an extra parameter, a plausible choice is the ellipse with minimum eccentricity. I haven’t looked at this in enough detail to prove it (the paper I cited proves something similar), but I believe this occurs when the source ellipse also has minimal eccentricity, which is obviously at $s=2$ when it’s the circle $(x'-1)^2+(y'-1)^2=1$.

If $\ell_1\parallel\ell_2$, the situation is a bit different. Now, the center of the ellipse is fixed at the midpoint of $\overline{P_1P_2}$ and what varies is the width of the circumscribed parallelogram. I’ll leave this case for you to work out yourself. It might even be possible to combine the two cases by working entirely in homogeneous coordinates.

Another possibility that I’ll sketch out here is to use “Plücker’s mu.” The idea is to construct two specific conics $Q$ and $Q'$ that satisfy the constraints. Every linear combination $\lambda Q+\mu Q'$ of these conics also satisfies those constraints. Given a point $\mathbf p$ that’s not one of our two fixed points, the unique conic of this family that also passes through $\mathbf p$ is $(\mathbf p^TQ\mathbf p)Q'-(\mathbf p^TQ'\mathbf p)Q$. For one of these generating conics you can take the degenerate conic $\mathbf l_1\mathbf l_2^T+\mathbf l_2\mathbf l_1^T$ that consists of the two tangent lines. A computationally convenient choice for the other conic is the unique parabola with the given tangents. If we set $\mathbf p_0$ to the intersection of the two lines, then this parabola is given in parametric form by the quadritic Bézier curve $(1-t)^2\mathbf p_1+2t(1-t)\mathbf p_0+t^2\mathbf p_2$. You can convert this into Cartesian form using the formula given here, and from there extract the coefficients to form the matrix of the parabola.

It might seem like you end up with two degrees of freedom this way, but you can restrict $\mathbf p$ to a ray that originates at $\mathbf p_0$ and lies “between” the tangents and still generate all of the possible ellipses.

It’s too bad that you didn’t explain what you were really trying to do in the first place. It would’ve saved quite a bit of time. Since what you want is an elliptical arc between two points with given tangents, I think that the most straightforward way to go is simply to map a parameterized quarter-circle to the destination points. Since affine transformations preserve tangents, this will give you an elliptical arc with the correct tangents that’s relatively simple to trace: the quarter circle is $\mathbf p'(t)=(1-\sin t,1-\cos t)$, which makes the arc $$x=x_0+(x_1-x_0)(1-\sin t)+(x_2-x_0)(1-\cos t) \\ y=y_0+(y_1-y_0)(1-\sin t)+(y_2-y_0)(1-\cos t).$$ As $t$ goes from $0$ to $\pi/2$, the arc is traced from $(x_1,y_1)$ to $(x_2,y_2)$.

If the curve doesn’t necessarily have to be elliptical, consider using a quadratic Bézier curve instead. This will be a parabolic arc with the same tangents and end points, but the curve is much easier to trace. With the same three points as above, the curve is $(1-t)^2\mathbf p_1+2t(1-t)\mathbf p_0+t^2\mathbf p_2$, $0\le t\le1$. There are well-known methods for drawing such curves that you can easily find on the Internet.

For the example in your answer, $\mathbf p_0\approx(14.7567,6.)$, making the elliptical arc $$x \approx 10.2343 - 1.23429 \cos t + 5.76571 \sin t \\ y= 13. - 7. \cos t,$$ which is illustrated below. For comparison, the broken blue line is the ellipse obtained by the original method with $s=3$ and the broken red line is the quadratic Bézier curve with those control points.

• P.S.: If we let $M'$ be the upper-right $2\times2$ submatrix of $M$ (the linear part) and $C=(s/2,s/2)$, then the vector equation of the image of the ellipse is $(\mathbf x-T(C))^T(M'^{-1})^TA(M'^{-1})(\mathbf x-T(C))=1$, with $A$ having $1/(s-1)$ on the main diagonal and $-(s-2)/(s(s-1))$ for the off-diagonal elements. This might be easier to compute than trying to transform the general equation at the top. – amd Aug 9 '16 at 5:43
• Thanks for your investigation, analysis, and suggested approach. Unfortunately, this is significantly beyond my current math skills, so I'd probably not be able to successfully implement anytime soon. Thanks again. – gkd720 Aug 9 '16 at 17:32
• @gkd720 It’s not so bad. You’ve got the equation (1) for an ellipse with parameter $s$ and a pair of equations for $(x,y)$ in terms of $(x',y')$. Solve those for $(x',y')$ and substitute back into (1). The algebra is a bit less messy if you substitute $\Delta x_1=x_1-x_0$ and so on in the coordinate transformation. Try it with the simple case $s=2$, where you just need to transform the equation of a circle. – amd Aug 9 '16 at 17:38
• OK, you guilt-ed me into it. From my 2 points, P1, and P2, I computed P0. Solving the mapping above for x' and y', I substituted back into (1). As suggested, to minimize the algebra, I substituted along the way whenever a constant expression showed up. Grouping terms, I ended up with the general equation (coefficients of A,B, etc.). I'll use wikipedia/Ellipse info to get the canonical form coefficients (a,b, Xc, Yc), then generate points for a center of 0, and offset by the real center, and plot. I'll report back if I ever get this coded up and working. Thanks again. – gkd720 Aug 11 '16 at 18:05
• @gkd720 It might be less of a hassle to work with the vector form of the equation. Once you have the transformed matrix $(M'^{-1})^TA(M'^{-1})$, you can diagonalize that to find the parameters of the transformed ellipse. – amd Aug 11 '16 at 21:11

Well, I made it through the messy algebra (confirmed by fixing a few errors along the way). I computed $P_0$, then solved for $x'$ and $y'$ and substituted back into $(1)$. I then manipulated it into the general conic form: $Ax^2+Bxy\ + ...\ \$. I then used wikipedia/Ellipse to get values for $a, b, X_c, Y_c$. I coded this up and ran it with an example ellipse segment and slopes. Looking at the result shows kind of what I expected, although the center seems slightly off, and the ellipse seems squished. Here's what I want to generate: ellipse segment The segment of interest is that from $(9,6)$ to $(16,13)$. At the first point, I want a slope of $0^\circ$, and at the second, a slope of $80^\circ$. I picked $s=3$. Running this results in a center of $(8,16.5)$ (marked in the picture with "C"). This seems about right, although I would have suspected the center X to be the first point's "9" since it had a slope of $0^\circ$. I then run X from 9 to 16, and generate the Y's. However, I would have expected the Y's to run from 6 to 13, but I only get a range from 8.5 to 11.2, hence "squished". Using the other sign value of the $\pm b$ in computing the Y values stretches the ellipse way too high. Does this sound like anything fundamentally wrong? Further lurking arithmetic/transcription errors suspected? I'll probably go through the manipulations again, as I stopped looking for them as soon as I stopped getting "sqrt argument out of domain" errors. So any further thoughts or guidance? Thanks.

• If you ended up with an $x$-coordinate of exactly $8$ for the center of the ellipse, then it’s not surprising that other things are off. Without seeing the details of what you’ve done, it sure sounds like there’s a fair amount of round-off error accumulating in your computations. As far as where the the center ends up relative to the two points, there’s no good reason to believe that the center should have the same $x$-coordinate as a point where the slope of the tangent is zero. Since you picked $s=3$, that’s not even true of the untransformed ellipse. – amd Sep 21 '17 at 4:31