4

I have the coefficients for a conic (I actually know that it is an ellipse) in the form $$Ax^2 + Bxy + Cy^2 + Dx + Ey + F =0$$

Is there an efficient algorithm which returns the parametrization of the eclipse, i.e., $\langle x(t), y(t) \rangle?$

Jbag1212
  • 1,435
  • What level of math do you know? Can I use calculus, linear algebra, trig? There are ways to do this that are mathematically more elegant, but require more math, and way that are more brutal, but require less math. – Doug M Jan 22 '18 at 23:05
  • You can use Multivariable Calculus, Linear Algebra, Differential Equations, and undergraduate level Analysis/Abstract Algebra. – Jbag1212 Jan 22 '18 at 23:08
  • Ultimately I want to write code which will do this, so I am focused on the efficiency of the algorithm. – Jbag1212 Jan 22 '18 at 23:08
  • Parametrizations are not unique. Depending on what you need it for it can be better to choose one over the other. What do you need it for? – orole Jan 22 '18 at 23:21
  • I need to "mark" the points which are inside an ellipse. Initially my code did a scan of every point in my 2d array and checked whether or not it was inside an ellipse, but this was very inefficient. What I want to try now is marking the boundaries of the ellipse, and then for each column/row in my array, say that if a point is between the two "edges" of the ellipse on that column/row, then it is inside the ellipse. – Jbag1212 Jan 22 '18 at 23:24
  • 2
    Then what you need is not a parametrization. The best way to know if a point is inside of the ellipse is to evaluate the implicit equation and check if the result is $\leq 0$. Your problem might be that you have too many points to test, with most lying outside. Then what you need is a way to cut down the number of points to test. For that it could be enough to compute the projection of the ellipse onto the X and Y axis. That will give you a box of points that are more likely to be inside, and then test only those. – orole Jan 22 '18 at 23:29
  • I don't think checking the implicit equation for $\leq 0$ is enough. See this https://math.stackexchange.com/questions/817130/how-to-determine-if-arbitrary-point-lies-inside-or-outside-a-conic. – Jbag1212 Jan 22 '18 at 23:32
  • The problem, like you said, is that I have too many points. If you know how to compute the projection of the ellipse onto the X and Y axis I would accept that as an answer. – Jbag1212 Jan 22 '18 at 23:33
  • 1
    @Jbag1212 That question was for a general conic, for which an appropriate sign choice of the general equation might not be obvious. For an ellipse, if you ensure that coefficient $A$ is positive, then checking the sign of the result of plugging the point into the l.h.s. of the general equation suffices. – amd Jan 22 '18 at 23:49
  • 1
    @amd Oh, that is helpful! It will speed up my code decently. To double check, once I have made sure that the coefficient $A$ is positive, then if l.h.s is $\leq 0$, the point is inside the ellipse, as orole initially said? – Jbag1212 Jan 23 '18 at 00:04
  • 1
    @Jbag1212 That’s right. If $A$ (and so also $C$) are positive, then there’s a sequence of translation, rotation and scale that will transform the ellipse’s equation into $x'^2+y'^2=1$. This transformation preserves the interior and exterior, so the direction of the distinguishing inequality is the same. – amd Jan 23 '18 at 01:00

2 Answers2

4

If all tools are at my disposal

Find the center. Take the partial derivatives.

$2Ax + By + D = 0\\ Bx + 2Cy + E = 0$

Solve this system of linear differential equations.

Lets call this $(x_0, y_0)$

$\begin{bmatrix} 2A & B\\B&2C \end{bmatrix}\begin {bmatrix} x_0\\y_0 \end{bmatrix} = \begin{bmatrix} -D\\-E\end{bmatrix}\\ \begin {bmatrix} x_0\\y_0 \end{bmatrix} = \frac {1}{B^2 - 4AC}\begin{bmatrix} 2CD-BE\\2AE - BD\end{bmatrix}$

$A(X-x_0)^2 + B(x-x_0)(y-y_0) + C(y-y_0)^2 = -F - Ax_0^2-Bx_0y_0 - C y_0^2$

Let $K^2 = -F - Ax_0^2-Bx_0y_0 - C y_0^2$

$\begin{bmatrix} (x-x_0)&(y-y_0)\end{bmatrix}\begin{bmatrix} A &\frac 12B\\\frac 12 B & C\end{bmatrix}\begin{bmatrix} (x-x_0)\\(y-y_0)\end{bmatrix} = K^2$

Diagonalize that matrix. The eigenvectors will be a rotation matrix. The eigenvalues will give you the coefficients. If it is an ellipse, the eigenvalues will both be positive.

Since you are trying to code this...

$\lambda^2 - (A+C) \lambda + AC-B^2 = 0\\ \lambda = \frac {(A+C)}{2} \pm \frac {\sqrt {(A-C)^2+4B^2}}{2}$

$\phi = \arctan {B}{A-\lambda_1} = \arctan \frac {C-\lambda_2}{B}$

(I hope I have that right)

$x = \frac {K}{\lambda_1} \cos (\theta-\phi) + x_0\\ y = \frac {K}{\lambda_2} \sin (\theta-\phi) + y_0$

Doug M
  • 57,877
  • if you look at the string of comments under the question, it appears the OP just wanted a way for a computer to decide rapidly whether a point given by $(x,y)$ coordinates is inside the fixed ellipse. Not entirely sure he knows how to confirm the quadratic part is positive definite, and that the ellipse is not the empty set or a single point. – Will Jagy Jan 23 '18 at 01:25
  • I don't think the final result include conics with $e \geq 1$. Any generalization to also include them? – Varad Mahashabde Mar 09 '20 at 13:14
  • Regardless OP would have wanted to, I like the deduction. But it doesn't work. The angle is best taken as $\phi=1/2 \mbox{arccot} ((A - C)/B)$ and, furthermore the parameterization in the $XY$ plane, is $R(\phi).(x'(t),y'(t))^T$, where $R(\phi)$ is the rotation matrix and $(x'(t), y'(t))$ corresponds to the formula you are giving. In addition, the eingevalues must be set correctly so that the parameterization matches the original ellipse. – wmora2 Jun 25 '22 at 23:14
2

Based on the comments to your question, it looks like the underlying problem that you’re trying to solve is to determine efficiently whether or not a point lies within the ellipse. A straightforward way to do this is to plug the coordinates of the point into the left-hand side of the general equation. The sign of the result will discriminate among the three possibilities. If you arrange for the leading coefficient $A$ to be positive (as will $C$ in that case), then the point is outside of the ellipse if the result is positive, inside if negative and of course on the ellipse if zero. Points very near the boundary might run into machine resolution limitations, so beware of that.

The reason this works is that, if you start from the unit circle $x^2+y^2=1$, for which the appropriate inequalities are obvious, any ellipse can be obtained from it via a combination of scaling, rotation and translation. This transformation preserves the interior and exterior, so the inequalities involving the transformed equation have the same senses. With positive scale factors, the coefficients $A$ and $C$ in the general equation will also be positive.

If you have a lot of points to test against a fixed ellipse and most of them are outside of the ellipse’s bounding box, it could be more efficient overall to do some preliminary range checks before evaluating the general conic expression. Finding the points on the ellipse where the partial derivative w/r $y$ vanishes will give you the $x$-coordinate bounds, while the points where the $x$-derivative vanishes will give you the $y$ bounds.

Another approach to finding the bounding box is to find the horizontal and vertical tangents to the ellipse. Working in homogeneous coordinates, the tangent lines to an ellipse given by the matrix $Q$ that go through a point $\mathbf p$ are captured by the degenerate conic $T=\mathcal M_{\mathbf p}^TQ^{-1}\mathcal M_{\mathbf p}$. Here, $\mathcal M_{\mathbf p}$ is the “cross-product matrix” of $\mathbf p$, i.e., $\mathcal M_{\mathbf p}\mathbf x=\mathbf p\times\mathbf x$. Instead of $Q^{-1}$, we can use the adjugate of $Q$ or any other convenient non-zero multiple of it instead.

Taking $\mathbf p = [0:1:0]$ will produce the vertical tangents to $Q$. For the general conic equation, then, we have $$Q = \begin{bmatrix}A&\frac B2&\frac D2\\\frac B2&C&\frac E2\\\frac D2&\frac E2&F \end{bmatrix}$$ and so $$T=\begin{bmatrix}0&0&-1\\0&0&0\\1&0&0\end{bmatrix} \begin{bmatrix} 4CF-E^2 & DE-4BF & 2BE-2CD \\ DE-4BF & 4AF-D^2 & 2BD-2AE \\ 2BE-2CD & 2BD-2AE & 4AC-4B^2 \end{bmatrix} \begin{bmatrix}0&0&1\\0&0&0\\-1&0&0\end{bmatrix} = \begin{bmatrix} 4AC-4B^2 & 0 & 2CD-2BE \\ 0&0&0 \\ 2CD-2BE & 0 & 4CF-E^2 \end{bmatrix}.$$ Now, you could use this directly for the range check by checking the sign of $(4AC-4B^2)\,x^2+(4CD-4BE)\,x+(4CF-E^2)$, but that’s not a whole lot better than simply using the original conic equation. However, we can tease out the individual lines by “splitting” this degenerate conic: Find an $\alpha$ for which $T+\alpha\mathcal M_{\mathbf p}$ has rank one. The two lines are then any row and column of the resulting matrix that have a common nonzero diagonal element. All of the $2\times2$ minors of $T+\alpha\mathcal M_{\mathbf p}$ are identically zero except for $$\alpha^2-4C^2(D^2-4AF)-4C(AE^2+4B^2F-2BDE).$$ Taking either root results in the equations $$2(AC-B^2)\,x = (BE-CD)\pm\sqrt{C^2(D^2-4AF)+C(AE^2+4B^2F-2BDE)}$$ for the tangents, which give you the left and right edges of the bounding box. The calculation for the top and bottom is similar, but with $\mathbf p=[1:0:0]$ instead, and again there’ll only be one $2\times2$ minor of $T+\alpha\mathcal M_{\mathbf p}$ that doesn’t vanish identically. The resulting equations for the horizontal tangents are $$2(AC-B^2)\,y=(BD-AE)\pm\sqrt{A^2(E^2-4CF)+A(CD^2+4B^2F-2BDE)}$$ from which we can read the max. and min. $y$-coordinates of the ellipse.

amd
  • 53,693
  • I'll be using this. To double check, I can use those bottom two quadratic equations for the $x$ and $y$ max and mins respectively? – Jbag1212 Jan 23 '18 at 19:00
  • @Jbag1212 Yes, those give you the max/min values, but the equations are linear in $x$ and $y$; everything else in them is a constant. Divide both sides by $4AC-B^2$ to get the values of $x$ and $y$. – amd Jan 23 '18 at 19:06
  • For the $x$ equation, I think there is a typo, it should be "$...+C(AE^2 + B^2 F - BDE)$", rather than "$...-C(AE^2 + B^2 F - BDE)$," am I correct? I am basing this comment off of experimenting with a few examples. – Jbag1212 Jan 25 '18 at 00:35
  • Also, I think there is a missing negative factor for the whole thing, in other words, when I am running this code, it is giving me that the $y$ minimum is the negative of the $y$ maximum, and the $y$ maximum is the negative of the $y$ minimum. (Which was an easy fix, but I thought I should mention it). – Jbag1212 Jan 25 '18 at 00:38
  • @Jbag1212 You’re right about the overall sign—lost it when I moved a term to the other side of the equals sign—but there was a more serious error, which is that there was a factor of 2 missing in a lot of places. I started from $Ax^2+2Bxy+Cy^2+2Dx+2Ey+F=0$, but the equation you are starting with doesn’t have those factors of two in the coefficients. I’ve made the appropriate corrections to match your form of the general equation. – amd Jan 25 '18 at 05:24