Assuming I have an ellipse with known center position (ex,ey) and size (ew,eh), how can I calculate the distance from x,y to the edge of the ellipse (in straight line from center)?

i.e. red line in the following image.

Note that the point can also be inside the ellipse (blue line).

Dist to point on ellipse

If it was a perfect circle the answer would be easy...

$d := |\sqrt{(x - ex)^2 + (y - ey)^2} - \text{radius}|$

  • $\begingroup$ "Edge of ellipse" is ambiguous. Do you want the intersection of a straight line to the point and the ellipse, or instead the closest point on the ellipse? $\endgroup$ May 12, 2020 at 0:42
  • $\begingroup$ Sorry, I mean the intersection of a straight line to the point and the ellipse center. $\endgroup$ May 12, 2020 at 0:44
  • $\begingroup$ The set up and solve the simultaneous equations. $\endgroup$ May 12, 2020 at 0:52

6 Answers 6


As a preliminary, the equation for an ellipse is $x^2/a^2+y^2/b^2=1$, where the length of the major (long) axis is $2a$.

If we are given the center of the ellipse and a point $P(x,y)$ outside of the ellipse, then we can calculate the distance to the edge as in your diagram by calculating the distance $d$ between point $P$ and the center of the ellipse, then subtracting the distance between the intersection and the center of the ellipse.

By the Pythagorean Theorem, $d=\sqrt{(x-c_x)^2+(y-c_y)^2}$, where $c_x$ and $c_y$ are the $x$ and $y$ coordinates of the center of the ellipse respectively. The slope $m$ of this segment is $\frac{y-c_y}{x-c_x}$, so now we have the equation of a line: $y=mx+b$, where we can find $b$ by substituting our point $P$ or the center of the ellipse $(c_x, c_y)$. We can now substitute this equation into our ellipse equation to find the point on the ellipse that the line intersects, and then use the Pythagorean Theorem again to find this distance, which we'll call $d_1$. Now, the solution is $d-d_1$.

  • $\begingroup$ Thanks David, that looks like it will solve my problem. $\endgroup$ May 12, 2020 at 7:04

TLDR: In the situation pictured below

ellipse variable names

$d_{diff} = \sqrt{ (p_x-c_x)^2 + (p_y-c_y)^2 } - \sqrt{\frac{r_x^2 \times r_y^2 \times ((p_x-c_x)^2 + (p_y-c_y)^2)}{(p_x-c_x)^2 \times r_y^2 + (p_y-c_y)^2 \times r_x^2}}$

or, in the case that the ellipse is centered at the origin ($c_x = 0$ and $c_y = 0$)

$d_{diff} = \sqrt{ p_x^2 + p_y^2 } - \sqrt{\frac{r_x^2 \times r_y^2 \times (p_x^2 + p_y^2)}{p_x^2 \times r_y^2 + p_y^2 \times r_x^2}}$

Be warned that this is not the shortest distance from the point to the edge of the ellipse. Still, the value being calculated here is important in certain situations.


equation 1: $\frac{p_x-c_x}{p_y-c_y} = \frac{e_x-c_x}{e_y-c_y}$ (The points lie on the same line)

equation 2: $\frac{(e_x-c_x)^2}{r_x^2} + \frac{(e_y-c_y)^2}{r_y^2} = 1$ (equation of an elipse)

equation 3: $d_{point} = \sqrt{(p_x-c_x)^2 + (p_y-c_y)^2}$ (distance between two points)

equation 4: $d_{ellipse} = \sqrt{(e_x-c_x)^2 + (e_y-c_y)^2}$ (distance between two points)

equation 5: $d_{diff} = d_{point} - d_{ellipse}$

  1. Solving for $e_y-c_y$ with equation 1: $e_y-c_y = \frac{(p_y-c_y) \times (e_x-c_x)}{p_x-c_x}$
  2. Solving for $e_x-c_x$ by substituting line 1 into equation 2: $e_x-c_x = (p_x-c_x) \times r_x \times r_y \times \sqrt{\frac{1}{(p_x-c_x)^2 \times r_y^2 + (p_y-c_y)^2 \times r_x^2}}$
  3. Solving for $e_y-c_y$ by substituting line 2 into equation 1: $e_y-c_y = (p_y-c_y) \times r_x \times r_y \times \sqrt{\frac{1}{(p_x-c_x)^2 \times r_y^2 + (p_y-c_y)^2 \times r_x^2}}$
  4. Solving for $d_{ellipse}$ by substituting lines 2 and 3 into equation 4: $d_{ellipse} = \sqrt{\frac{r_x^2 \times r_y^2 \times ((p_x-c_x)^2 + (p_y-c_y)^2)}{(p_x-c_x)^2 \times r_y^2 + (p_y-c_y)^2 \times r_x^2}}$
  5. Solving for $d_{diff}$ by substituting line 4 into equation 5: $d_{diff} = \sqrt{(p_x-c_x)^2 + (p_y-c_y)^2} - \sqrt{\frac{r_x^2 \times r_y^2 \times ((p_x-c_x)^2 + (p_y-c_y)^2)}{(p_x-c_x)^2 \times r_y^2 + (p_y-c_y)^2 \times r_x^2}}$


I tried generating some values and solutions for this problem in some CAD software, checked them against substituting $cos(\theta) = \frac{x}{\sqrt{x^2+y^2}}$ and $sin(\theta) = \frac{y}{\sqrt{x^2+y^2}}$ into user765629's equation and it did not produce the correct answer. I did the same for the equation I produced and it produced the correct answer.


Well there is a formula but it is going to be long and messy.

Take the ellipse $\frac{(x-a)^2}{A^2}+\frac{(y-b)^2}{B^2}=1$

The centre is $(a,b)$, with semiminor $A$ and semimajor $B$.

Take the point $(p,q)$. It doesn't matter if it's inside, outside or on the ellipse.

Step 1: Derive the line through $(a,b)$ and $(p,q)$ in the form $y=gx+h$

Step 2: Find the point of contact between the line and the ellipse.

Sub this expression for $y$ into your expression for the ellipse.

The only unknown is $x$ so you can solve for $x$.

Sub your value for $x$ into the line expression to find $y$.

Now you have the point of intersection.

Step 3: Find the distance from $(p,q)$ to your new point using the distance formula.


Let us consider a point in the plane, $P = (a,b)$, a center of an ellipse, $C=(x_0,y_0)$, and the point on the edge of the ellipse that the line $PC$ intersects, $E=(x,y)$. The general equation of an ellipse centered at $(x_0,y_0)$ is given by $$ \frac{(x-x_0)^2}{r_x^2}+\frac{(y-y_0)^2}{r_y^2}=1 $$ where $r_x$ is the $x$-radius and $r_y$ is the $y$-radius. The above can also be parameterised by $$ x=x_0+r_x\cos(\theta),\quad y=y_0+r_y\sin(\theta),$$ where $\theta$ can be interpreted as the angle between the $x$-radius $r_x$ and the line connecting the center to the edge $CE$.

By Pythagoras, given any point $P$, the distance to the center of the ellipse $C$ is $d_{PC}=\sqrt{(a-x_0)^2+(b-y_0)^2}$. It is fairly straight forward that the distance from $P$ to $E$ is $d_{PE} = |d_{PC} - d_{EC}|$.

To find $d_{EC}$, we require to find the angle $\theta$. You must consider the correct quadrant w.r.t the ellipse center. If it is in the first quadrant (top right), then $\theta=\arctan\left(\frac{b-y_0}{a-x_0}\right)$ (soh-cah-toa will get you here). Otherwise you have to do a little bit of manipulating, or you could use $\textrm{atan}2(b-y_0, a-x_0)$ (https://en.wikipedia.org/wiki/Atan2). Again, by Pythagoras, we then find that $$ d_{EC}^2 = (x-x_0)^2+(y-y_0)^2=r_x^2\cos^2(\theta) + r_y^2\sin^2(\theta).$$ And from above we can find $d_{PE}$ as desired. So the final distance is given by, $$d_{PE} = \left|\sqrt{(a-x_0)^2+(b-y_0)^2} - \sqrt{r_x^2\cos^2(\theta) + r_y^2\sin^2(\theta)}\right|. $$ This is the same as the formula you wrote for a circle, but now the radius (the second term in above) is dependent on angle. Note also that if $r_x=r_y$ the above simplifies to the formula for a circle. I hope this helps.

  • $\begingroup$ I used phi instead of θ in the last 2 equations phi = atan2(rx * sin(θ), ry * cos(θ)) $\endgroup$
    – Lefteris E
    Jul 3, 2022 at 19:44
  • $\begingroup$ I checked this equation against some CAD software generated values and answers and it did not produce the same answers. $\endgroup$ Feb 10 at 7:43

Distance to pt with respect to center:

$$ d= \sqrt {(y-ey)^2+(x-ex)^2} $$

Ellipse in Polar Coordinates answer

Polar coordinates should be employed for pt with respect to center:

$$\theta= \tan^{-1}\dfrac{y-ey}{x-ex},\quad 2 r_{polar}= \frac{ew\cdot eh}{\sqrt{(eh \cos \theta)^2 + (ew \sin \theta)^2}}\tag {1*} $$

and the required distance (depending on whether pt is outside or inside of ellipse) is directly:

$$ \pm(d-r_{polar}). $$

  1. Find line f(x) that passes through (x,y) and (ex,ey):

$$ m = \frac{y-ey}{x-ex} \\ i = y-mx \\ f(x) = mx+i $$

  1. Find intersection point - after a number of calculations we have:

$$ \theta = \left(\frac{eh}{ew}\right)^2 \\ A=m^2+\theta \\ B=2(mi-m \cdot ey-\theta ex) \\ C=i^2+ey^2-2i \cdot ey - eh^2 + \theta ex^2 \\ \Delta = B^2-4AC \\ x_1=\frac{-B-\sqrt{\Delta}}{2A} \\ x_2=\frac{-B+\sqrt{\Delta}}{2A} $$

And now your distance is $\min(x_1,x_2)$


You must log in to answer this question.

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