3
$\begingroup$

I have 2 points and I want to draw an ellipse that passes through those points.
I also have the center of the ellipse.

My question is: is it always possible to draw such an ellipse?

$\frac{(x-c_x)^2}{a^2} + \frac{(y-c_y)^2}{b^2} = 1$

When trying to automatically render ellipses for information visualization, I tried calculating the radiuses $a$ and $b$ by solving the two-equation system. However, sometimes $a^2$ or $b^2$ would be negative and, therefore, $a$ or $b$ would return me $NaN$.

Edit: A test case where this is failing is with:
$P_1 = (610, 320)$
$P_2 = (596, 887)$
$C = (289, 648)$

Edit2: I'm sorry, I made it seem like a completly theoretical question but in fact I need to overcome this limitation. The suggestion in the comments involves a "tilted ellipse", but how exactly can I get its parameters with two points and the center?

$\endgroup$
  • 3
    $\begingroup$ If the two points are collinear with the center, then they obviously need to be at the same distance from the center, implying that this is not always possible. $\endgroup$ – Jyrki Lahtonen Mar 3 at 13:31
  • $\begingroup$ Also, are you only interested in ellipses with their semiaxes parallel to the coordinate axes? If you allow a tilted ellipse, then the equation is more general. There will be a mized term of the form a constant times $(x-c_x)(y-c_y)$ (and a quadratic inequality that the coefficients must satisfy to get an ellipse as opposed to a hyperbola. $\endgroup$ – Jyrki Lahtonen Mar 3 at 13:33
  • $\begingroup$ @JyrkiLahtonen I've added my test case and as you can see they are not collinear, so why exactly is it impossible in this case? The framework I use only allows for ellipses with their semiaxes parallel to the coordinate axes. However, I am able to apply common geometric transformations to it, like translation, rotation and scale. $\endgroup$ – Daniel Marques Mar 3 at 13:41
  • 4
    $\begingroup$ If you allow a tilted ellipse, then collinearity is the only problem. In that case it is easy to see that you can always select the more distant point (from the center), say $P_1$, to be at the end of the longer semiaxes. The third point is inside the circle with center at $C$ and radius $CP_1$. Scaling that circle linearly in the direction perpendicular to $CP_1$ then gives you the ellipse you need. If you constrain the directions of the semiaxes, it is more complicated. $\endgroup$ – Jyrki Lahtonen Mar 3 at 13:56
  • $\begingroup$ @JyrkiLahtonen Sorry, but I didn't quite get it... The ellipse needs two perpendicular axes, correct? The longest would be from $C$ to $P_1$. The part that I didn't get was how would I calculate the other axis. $\endgroup$ – Daniel Marques Mar 3 at 14:42
3
$\begingroup$

As other answers have pointed out, it’s not always possible to draw an axis-aligned ellipse with center $C$ that passes through the points $P_1$ and $P_2$. If you relax the axis alignment requirement, then if the three points $C$, $P_1$ and $P_2$ are not colinear it’s always possible to draw an ellipse; it will in general not be axis-aligned, however. In fact, there is an infinite number of such ellipses.

As you’ve noted, one possibility is to take the line through $C$ and the farther of the two other points from it as the major axis. The parameters of the ellipse can be computed fairly easily by applying a change of coordinates that makes the major axis the $x$-axis and places $C$ at the origin.

Another possibility is to take $CP_1$ and $CP_2$ as conjugate half-diameters of the ellipse, which leads to the parameterization $$C+(P_1-C)\cos t+(P_2-C)\sin t.$$ If you need an implicit Cartesian equation of this ellipse, you can (somewhat painstakingly) eliminate $t$. The parameters of the ellipse can then be extracted from the general conic equation using standard formulas. Alternatively, we can use the fact that at the vertices, the radius and tangent vectors are orthogonal. This condition leads to the equation
$$2(P_1-C)\cdot(P_2-C)\cos{2t} = \left(\lVert P_1-C\rVert^2 - \lVert P_2-C\rVert^2\right) \sin{2t}.$$ Setting $s$ and $c$ to the coefficients of $\cos{2t}$ and $\sin{2t}$, respectively, we have $$\cos{2t} = {c\over\sqrt{c^2+s^2}}, \sin{2t} = {s\over\sqrt{c^2+s^2}},$$ and you can use standard half-angle formulas to compute $\cos t$ and $\sin t$ and then find the ellipse’s semiaxis vectors.

For your example, this produces the following ellipse:

ellipse

Its semiaxis lengths work out to be approximately $465.651$ and $381.004$, and the major axis has approximate direction $(0.853,-0.522)$, which corresponds to an angle of $-31.48°$ from the $x$-axis.

$\endgroup$
5
$\begingroup$

Let's translate the coordinates so that the centre is at the origin. The two given points are then $Q_1 = P_1-C = (321, -328)$ and $Q_1 = P_2-C = (307, 239)$.

Since the centred ellipse will be symmetric w.r.t. the axes, we can ignore the minus sign and use $Q_1' = (321, 328)$ instead, as that will also need to be on the ellipse. The two points are now both in the first quadrant.

If you connect any two points in the first quadrant of an origin-centred axis-aligned ellipse, you will get a line with a negative slope. Or put another way, one of the points has the largest x-coordinate, and the other then must have the largest y-coordinate. Conversely, if you have any two points with positive coordinates, one with a largest x-coordinate and the other with the largest y-coordinate, then there is an origin-centred axis-aligned ellipse that goes through them.

This is not the case here with the two points $(321, 328)$ and $(307, 239)$. The first point has both coordinates larger than the other. So no origin-centred axis-aligned ellipse is possible through those points.

$\endgroup$
  • $\begingroup$ You get a hyperbola, then. Right? You may want to mention this. $\endgroup$ – Oscar Lanzi Mar 3 at 15:00
  • $\begingroup$ @Jaap Scherphuis What if I translate the three points so that the center $C$ is at the origin and then rotate the points by such an angle $\alpha$ that one of the points coincides with an axis? Then would it always be possible to get an ellipse (not considering the collinearity problem)? I ask this because then I could simply rotate back ($-\alpha$) and translate back to the original center. $\endgroup$ – Daniel Marques Mar 3 at 15:12
  • 2
    $\begingroup$ @DanielMarques Yes, that is correct. That gives the same ellipse as what Jyrki Lahtonen was describing in his second comment. It only works if you put the furthest point on an axis, cause otherwise you still are in the position I described in my answer. There are other angles that allow an ellipse - any angle that makes $|x_1|>|x_2|$ and $|y_2|>|y_1|$, or vice versa. $\endgroup$ – Jaap Scherphuis Mar 3 at 15:36
  • $\begingroup$ @JaapScherphuis Hello again, I'm sorry to ask you this but I tried putting the knowledge I learned with these answers and comments into use and implement this in Three.js. Unfortunatelly I didn't work for some cases, would you please read my question and see if you spot something wrong? stackoverflow.com/questions/60835159/… $\endgroup$ – Daniel Marques Mar 24 at 16:38
2
$\begingroup$

$b^2(x_1-c)^2+a^2(y_1-d)^2=(ab)^2$ $b^2(x_2-c)^2+a^2(y_2-d)^2=(ab)^2$

The difference $$b^2(x_1-x_2)(x_1+x_2-2c)+a^2(y_1-y_2)(y_1+y_2-2d)=0$$ note that $x_1,x_2,y_1,y_2,c,d$ are given so we find a relation between $a^2,b^2$ we use that in the first or second equation to find the $a,b$ values we take positive values. If $a^2,b^2$ are negative then there is no solution!

Let translate the ellipse by make the center $(0,0)$ in your example $C(0,0)$ $P_1(321,-328)$ $P_2(307,239)$ Now $a$ is the largest horizontal distance possible it should be bigger than $321$ and $b$ vertical radius should be bigger than $328$. What I was thinking is it is not always possible to draw an ellipse knowing the center and two other points.

Edited:As @Oscar Lanzi pointed out if $a^2,b^2$ are negative then we have a hyperbola

$\endgroup$
  • 1
    $\begingroup$ "If $a^2, b^2$ is negative then there is no solution!" Geometrically there is a solution -- but it's a hyperbola instead of an ellipse. Possibly put that in? $\endgroup$ – Oscar Lanzi Mar 3 at 15:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.