74
$\begingroup$

I have an ellipse centered at $(h,k)$, with semi-major axis $r_x$, semi-minor axis $r_y$, both aligned with the Cartesian plane.

How do I determine if a point $(x,y)$ is within the area bounded by the ellipse?

$\endgroup$
6
  • $\begingroup$ Which of the two solutions is more efficient (computationally-wise) assuming that in both cases the "|x−h|>rx" and "|y−k|>ry" rejections are implemented? $\endgroup$ Commented Oct 15, 2012 at 13:19
  • 2
    $\begingroup$ @rsanchezsaez Probably Srivatsan's because square roots are slow; if you're concerned about performance writing both and benching them is probably the best route. Alternately, you could try posing the question on Stack Overflow. $\endgroup$ Commented Oct 15, 2012 at 14:27
  • $\begingroup$ Thank for the tips Dan. I implemented Srivatsan's and seems to work fine. I don't want to spend much time in premature optimization. If in the future we run into performance issues we will profile to see if this is the bottleneck. $\endgroup$ Commented Oct 15, 2012 at 15:00
  • $\begingroup$ "Aligned with the Cartesian plane" means that the major and minor axes lie on the coordinate axes, right? $\endgroup$
    – rschwieb
    Commented Feb 5, 2013 at 17:41
  • 1
    $\begingroup$ Check whether a point lies inside a rotated ellipse $\endgroup$
    – Sen Jacob
    Commented Dec 20, 2016 at 14:50

4 Answers 4

132
$\begingroup$

The region (disk) bounded by the ellipse is given by the equation: $$ \frac{(x-h)^2}{r_x^2} + \frac{(y-k)^2}{r_y^2} \leq 1. \tag{1} $$ So given a test point $(x,y)$, plug it in $(1)$. If the inequality is satisfied, then it is inside the ellipse; otherwise it is outside the ellipse. Moreover, the point is on the boundary of the region (i.e., on the ellipse) if and only if the inequality is satisfied tightly (i.e., the left hand side evaluates to $1$).

$\endgroup$
7
  • 12
    $\begingroup$ I have just registered to upvote this. I am developing app for android and this has been EXACTLY what I was looking for. Thank u $\endgroup$
    – Eugene
    Commented Apr 4, 2013 at 7:56
  • 1
    $\begingroup$ What does h and k represent, is it the origin of the ellipse? $\endgroup$
    – Dave
    Commented Nov 1, 2014 at 23:55
  • 2
    $\begingroup$ @Dave, yes h and k represent the origin of the ellipse. $\endgroup$
    – rein
    Commented Jan 22, 2015 at 20:22
  • 1
    $\begingroup$ I'm with @Eugene in that I'm trying to develop an app for iOS and Android. Neither seem to be able to work with ellipse bounds detection. $\endgroup$ Commented Oct 29, 2016 at 15:58
  • 9
    $\begingroup$ Note that if you're coding this, you should multiply both sides by $r_x^2 * r_y^2$ (which can't be negative so it can't flip the inequality). Computers can multiply much more efficiently than they can divide. $\endgroup$ Commented Mar 15, 2017 at 10:11
10
$\begingroup$

Another way uses the definition of the ellipse as the points whose sum of distances to the foci is constant.

Get the foci at $(h+f, k)$ and $(h-f, k)$, where $f = \sqrt{r_x^2 - r_y^2}$.

The sum of the distances (by looking at the lines from $(h, k+r_y)$ to the foci) is $2\sqrt{f^2 + r_y^2} = 2 r_x $.

So, for any point $(x, y)$, compute $\sqrt{(x-(h+f))^2 + (y-k)^2} + \sqrt{(x-(h-f))^2 + (y-k)^2} $ and compare this with $2 r_x$.

This takes more work, but I like using the geometric definition.

Also, for both methods, if speed is important (i.e., you are doing this for many points), you can immediately reject any point $(x, y)$ for which $|x-h| > r_x$ or $|y-k| > r_y$.

$\endgroup$
1
  • 2
    $\begingroup$ Right, it's easier to check if a point is within a rectangle. If it isn't in the rectangle, it certainly can't be in the ellipse. $\endgroup$ Commented Oct 28, 2011 at 7:16
9
$\begingroup$

1) Consider the point as a vector

$$ p=\begin{bmatrix} x \\ y \\ \end{bmatrix} $$

2) Consider the center of the ellipse as

$$ c=\begin{bmatrix} h \\ k \\ \end{bmatrix} $$

3) Subtract the center of the ellipse $$ p_{centered}= p - c $$ 4) Create a whitening matrix $$ W = \Lambda^{-1/2}E^T $$ where $$ \Lambda = \begin{bmatrix} r_x & 0 \\ 0 & r_y \\ \end{bmatrix} $$ and $$ E = \begin{bmatrix} e_{major} & e_{minor}\\ \end{bmatrix} $$ where $e_{major}$ and $e_{major}$ are the unit vectors in the direction of the ellipse's major and minor axes. Since you're example is for a non-rotated matrix with major axis along x-axis and minor axis along y-axis

$e_{major}=\begin{bmatrix} 1\\ 0\\ \end{bmatrix}$ and $e_{minor}=\begin{bmatrix} 0\\ 1\\ \end{bmatrix}$

5) Whiten the point $$ p_{white} = Wp_{centered} $$ 6) Check if the length of the vector is less than 1. If it is, then the point is within the ellipse.

Note: this is inspired by my experience with Covariance matrices. I'll try to update this answer with an intuitive relation b/w ellipses and covariance matrices. For now you can take a peak at http://www.visiondummy.com/2014/04/draw-error-ellipse-representing-covariance-matrix/

$\endgroup$
4
$\begingroup$

I have another solution. In summary, transform everything so you can test whether a point is within a circle centered at (0,0). Since the ellipse is oriented orthogonally to the Cartesian plane, we can simply scale one of the dimensions by the quotient of the two axes.

First subtract (h,k) from both points.

(h,k) becomes (0,0). (x,y) becomes (x-h,y-k).

Now we scale the second coordinate, normalizing it to the first.

Scaling (x-h,y-k) we get $$ (x-h,(y-k) * \frac{r_x}{r_y}) $$

Now we simply need to test if $$ |(x-h,(y-k) * \frac{r_x}{r_y})| <= r_x $$

$\endgroup$
2
  • $\begingroup$ I apologize. The coding of the fractions didn't take. Perhaps someone can help me out with that and point me to a reference? I was using meta.math.stackexchange.com/questions/5020/… as a guide. Edit: I figured it out. I was missing the \$$ tag. $\endgroup$ Commented Nov 24, 2012 at 3:45
  • $\begingroup$ This "move everything to 0,0" is a trick also heavily favored by my former math tutor, who was a mathematician in the age just prior to the explosion of graphing calculators. She was used to having to solve everything by hand on paper, so collected "quick and dirty" tricks for simplifying problems down to practical size. $\endgroup$
    – Tom
    Commented Sep 28, 2023 at 22:28

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .