TITLE: Detect if two ellipses intersect
QUESTION [5 upvotes]: I have seen a lot of papers on how to find points of intersection between two ellipses for 2D case, but i only need to check if two ellipses are in collision. I don't need to know points of intersection if there are any. Is there simplified algorithm for this. Thanks.
I know center and two radii for every ellipse. Both ellipses can be rotated.

REPLY [3 votes]: Checking whether two ellipses intersect, or more generally two n-dimenensional ellipsoids, can be done by solving a 1D smooth convex optimization problem. This is very easy and fast to do computationally.
Format the ellipsoids are given in
First, the data format that ellipsoids are given in should be a a vector which defines the center of the ellipse, and a symmetric positive definite matrix which defines the shape of the ellipse. In particular, let $a,b \in \mathbb{R}^n$, let $A,B \in \mathbb{R}^{n \times n}$ be symmetric positive definite matrices, and define the ellipsoids as follows:
\begin{align*}
E_A &= \{x : (x-a)^T A (x-a) \le 1\} \\
E_B &= \{x : (x-b)^T B (x-b) \le 1\}.
\end{align*}
The vector $a$ is the centerpoint of the ellipsoid $E_A$. The eigenvectors of the matrix $A$ are the unit vectors that point in the directions of the primary axes of $E_A$. The inverse square roots of the corresponding eigenvalues ($1/\sqrt{\lambda_i}$) of $A$ are the lengths of the primary axes of $E_A$. This is shown in the image below. The vector $b$ and matrix $B$ characterize $E_B$ in the same manner.

The vectors $a$ and $b$ and matrices $A$ and $B$ uniquely define the ellipsoids $E_A$ and $E_B$, respectively. Also, for every ellipsoid, there is a corresponding vector and matrix (like $a$ and $A$) that can put it in this format. For more on this, you can see the following wikipedia article:
https://en.wikipedia.org/wiki/Ellipsoid
If your ellipses (2D) or ellipsoids (nD) are given in another format, such as axis lengths and angles, the first step is to convert them to this format by computing the vectors $a$ and $b$, and matrices $A$ and $B$.
Fast ellipsoid intersection test
Now define the following convex scalar function $K:(0,1)\rightarrow \mathbb{R}$,
$$K(s) := 1 - (b-a)^T\left(\frac{1}{1-s}A^{-1} + \frac{1}{s}B^{-1}\right)^{-1}(b-a).$$
The following result holds:
$$E_A \cap E_B = \{\} \quad \text{if and only if}\quad K(s)<0 \quad \text{for some}\quad s \in (0,1).$$
This is proven in Proposition 2 of the following paper:

Gilitschenski, Igor, and Uwe D. Hanebeck. "A robust computational test
for overlap of two arbitrary-dimensional ellipsoids in fault-detection
of kalman filters." 2012 15th International Conference on Information
Fusion. IEEE, 2012.

So you can check whether the two ellipsoids intersect by finding the minimum of $K$ on $(0,1)$ using any 1D convex minimization routine, then check if $K(s)$ is greater than 0 at the minima.
I like to use Brent's algorithm to find the minimum, because it is fast and implementations are available in many software libraries such as scipy.
https://en.wikipedia.org/wiki/Brent%27s_method
The minimization algorithm will evaluate $K(s)$ many times for different values of $s$. You can avoid performing matrix inverses/linear solves at each evaluation of $K(s)$ by precomputing the generalized eigenvalue decomposition of $A$ with respect to $B$, then working in the basis of generalized eigenvectors, because both matrices become diagonal in this basis.
http://fourier.eng.hmc.edu/e161/lectures/algebra/node7.html
Code
Here is some python code I wrote to do this. In this code, $\Sigma_A := A^{-1}$, $\Sigma_B := B^{-1}$, $\mu_A = a$, and $\mu_B = b$, and $\tau$ is an optional scalar that makes the ellipses uniformly bigger or smaller.
import numpy as np
from scipy.linalg import eigh
from scipy.optimize import minimize_scalar


def ellipsoid_intersection_test(Sigma_A, Sigma_B, mu_A, mu_B, tau=1.0):
    lambdas, Phi = eigh(Sigma_A, b=Sigma_B)
    v_squared = np.dot(Phi.T, mu_A - mu_B) ** 2
    res = minimize_scalar(K_function,
                          bracket=[0.0, 0.5, 1.0],
                          args=(lambdas, v_squared, tau))
    return (res.fun[0] >= 0)


def K_function(s, lambdas, v_squared, tau):
    return 1.-(1./tau**2)*np.sum(v_squared*((s*(1.-s))/(1.+s*(lambdas-1.))))

Example images of $K(s)$
Here are a sequence of pictures showing how the function $K(s)$ changes as one ellipsoid slides past another:









The complete code I used to generate these pictures is in the following jupyter notebook:
https://github.com/NickAlger/nalger_helper_functions/blob/master/tutorial_notebooks/ellipsoid_intersection_test_tutorial.ipynb