$ \def \J {\Delta} \def \half {\frac{1}{2}} \def \kwart {\frac{1}{4}} \def \hieruit {\quad \Longrightarrow \quad} \def \slechts {\quad \Longleftrightarrow \quad} \def \norm {\frac{1}{\sigma \sqrt{2\pi}} \; } \def \EN {\quad \mbox{and} \quad} \def \OF {\quad \mbox{or} \quad} \def \wit {\quad \mbox{;} \quad} \def \spekhaken {\iint} \newcommand{\dq}[2]{\displaystyle \frac{\partial #1}{\partial #2}} \newcommand{\oq}[2]{\partial #1 / \partial #2} \newcommand{\qq}[3]{\frac{\partial^2 #1}{{\partial #2}{\partial #3}}} \def \erf {\operatorname{Erf}} $

Intersecting an Ellipse

The following expression will be proposed for the bounding ellipse of a curve: $$ \sigma_{yy} (x-\mu_x)^2 - 2\, \sigma_{xy} (x-\mu_x) (y-\mu_y) + \sigma_{xx} (y-\mu_y)^2 - E = 0 $$ Bounding ellipses are employed in our programs for clipping complete contours "at first sight", that is: without having to traverse all vertices of these contours. For this purpose, they are far more handsome than bounding boxes.
In order to make this clipping machinery operational, we have to answer the following question. What are the intersection points of a bounding ellipse with an arbitrary straight line segment: $$ \begin{array}{c} x = x_1 + \lambda (x_2 - x_1) \\ y = y_1 + \lambda (y_2 - y_1) \end{array} \quad \mbox{where} \quad 0 \le \lambda \le 1 $$ Substitute $x$ and $y$ in the equation for the ellipse: $$ \sigma_{yy} \left[ \lambda (x_2-x_1) + (x_1-\mu_x) \right]^2 $$ $$ - 2\, \sigma_{xy} \left[ \lambda (x_2-x_1) + (x_1-\mu_x) \right] \left[ \lambda (y_2-y_1) + (y_1-\mu_y) \right] $$ $$ + \sigma_{xx} \left[ \lambda (y_2-y_1) + (y_1-\mu_y) \right]^2 - E = 0 $$ And work out: $$ \left[ \sigma_{yy} (x_2-x_1)^2 - 2\, \sigma_{xy} (x_2-x_1)(y_2-y_1) + \sigma_{xx} (y_2-y_1)^2 \right] \lambda^2 $$ $$ + \, 2 \left[ \sigma_{yy} (x_2-x_1)(x_1-\mu_x) - \sigma_{xy} (x_2-x_1)(y_1-\mu_y) \right. $$ $$ \left. - \sigma_{xy} (y_2-y_1)(x_1-\mu_x) + \sigma_{xx} (y_2-y_1)(y_1-\mu_y) \right] \lambda $$ $$ + \left[ \sigma_{yy} (x_1-\mu_x)^2 - 2\, \sigma_{xy} (x_1-\mu_x)(y_1-\mu_y) + \sigma_{xx} (y_1-\mu_y)^2 \right] - E = 0 $$ This is a quadratic equation: $$ A \lambda^2 + 2\,B\lambda + C = 0 $$ Where: \begin{eqnarray*} A &=& \sigma_{yy} x_{21}^2 - 2\, \sigma_{xy} x_{21} y_{21} + \sigma_{xx} y_{21}^2 \\ B &=& \sigma_{yy} x_{21} x_{1\mu} - \sigma_{xy} \left[ x_{21} y_{1\mu} + y_{21} x_{1\mu} \right] + \sigma_{xx} y_{21} y_{1\mu} \\ C &=& \sigma_{yy} x_{1\mu}^2 - 2\, \sigma_{xy} x_{1\mu} y_{1\mu} + \sigma_{xx} y_{1\mu}^2 - E \end{eqnarray*} The following substitutions have been done: $$ x_{21} = x_2-x_1 \qquad y_{21} = y_2-y_1 \qquad x_{1\mu} = x_1-\mu_x \qquad y_{1\mu} = y_1-\mu_y $$ Whether the ellipse is intersected by the line is determined by the discriminant $D$ in the first place: \begin{eqnarray*} && D = B^2 - A\,C < 0 \hieruit \mbox{no intersections} \\ && D = B^2 - A\,C = 0 \hieruit \mbox{maybe tangent} \\ && D = B^2 - A\,C > 0 \hieruit \mbox{maybe intersecting} \end{eqnarray*} In case $B^2 - A\,C = 0$, the tangent point $(x,y)$ is given by: $$ \begin{array}{c} x = x_1 + \lambda (x_2 - x_1) \\ y = y_1 + \lambda (y_2 - y_1) \end{array} \quad \mbox{where} \quad \lambda = - \frac{B}{A} \quad \mbox{but only if} \quad 0 \le \lambda \le 1 $$ In case $B^2 - A\,C > 0$, the intersection points $(x,y)$ are given by: $$ \begin{array}{c} x = x_1 + \lambda (x_2 - x_1) \\ y = y_1 + \lambda (y_2 - y_1) \end{array} \quad \mbox{where} \quad \lambda = \frac{-B \pm \sqrt{D}}{A} \quad \mbox{but only if} \quad 0 \le \lambda \le 1 $$ The above provides (more than) enough material to efficiently implement the clipping of complete contours. For example, if the discriminant of a clipping line and an ellipse is negative, then all vertices of a contour are clipped if only the midpoint $(\mu_x,\mu_y)$ of its bounding ellipse is clipped.