Calculate Distance from Point to Ellipse edge

geometry

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}|$

Best Answer

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$.

Related Question