# Calculate Distance from Point to Ellipse edge

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). If it was a perfect circle the answer would be easy...

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

• "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? May 12 '20 at 0:42
• Sorry, I mean the intersection of a straight line to the point and the ellipse center. May 12 '20 at 0:44
• The set up and solve the simultaneous equations. May 12 '20 at 0:52

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

• Thanks David, that looks like it will solve my problem. May 12 '20 at 7:04

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.

Distance to pt with respect to center:

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

$$\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*}$$
$$\pm(d-r_{polar}).$$