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?
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:
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.